hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.83k stars 9.18k forks source link

Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found when using The Module providers Meta-Argument #18065

Open cesarpball opened 3 years ago

cesarpball commented 3 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

terraform -v Terraform v0.13.5

Affected Resource(s)

Terraform Configuration Files

Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.

# Main module configuration
provider "aws" {
  version                 = "~> 3.31.0"
  #region                  = var.aws-region
  region = "eu-west-1"
  shared_credentials_file = "/Users/pepebotika/.aws/credentials"
  profile = "prod"
  alias                   = "prod"
  allowed_account_ids = [var.prod]
}

provider "aws" {
  version                 = "~> 3.31.0"
  #region                  = var.aws-region
  region = "eu-west-1"
  shared_credentials_file = "/Users/pepebotika/.aws/credentials"
  profile                 = "dev"
  alias                   = "dev"
   # Only these AWS Account IDs may be operated on by this template
  allowed_account_ids = [var.dev]
}

Calling the child module

module "test" {

 providers = {
  aws.src = aws.prod
  aws.dst = aws.dev

 }
  source                 = "git::myrepo"
  ...
  ...
}

On the child module my repo:

provider "aws" {
  alias = "src"
  region = "eu-west-1"
}

provider "aws" {
  alias = "dst"
  region = "eu-west-1"
}

Debug Output

Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.

Error: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2021/03/12 08:34:52 [ERROR] eval: *terraform.EvalOpFilter, err: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.

Error: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors
2021/03/12 08:34:52 [ERROR] eval: *terraform.EvalSequence, err: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.

Panic Output

Expected Behavior

terraform plan and terraform apply executed succesfully

Actual Behavior

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.

Error: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

  on .terraform/modules/myrepo/providers.tf line 1, in provider "aws":
   1: provider "aws" {

Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found.

Please see https://registry.terraform.io/providers/hashicorp/aws
for more information about providing credentials.

Error: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors

  on .terraform/modules/myrepo/providers.tf line 6, in provider "aws":
   6: provider "aws" {

Steps to Reproduce

  1. terraform init
  2. terraform plan

References

cesarpball commented 3 years ago

Quick update:

Calling the child module:

module "test" {

 providers = {
  aws = aws.prod
 }
  source                 = "git::myrepo"
  ...
  ...
}

Inside of child module:

provider "aws" {
  alias = "src"
  region = "eu-west-1"
}

provider "aws" {
  alias = "dst"
  region = "eu-west-1"
}

terraform validate Success! The configuration is valid.

Now... back to the original and calling the provider aws.src instead of aws

module "test" {

 providers = {
  **aws.src** = aws.prod
 }
  source                 = "git::myrepo"
  ...
  ...
}

terraform validate

Error: Missing required argument

The argument "region" is required, but was not set.

Then if I

AWS_DEFAULT_REGION=eu-west-1 terraform validate
Success! The configuration is valid.

but If I

AWS_DEFAULT_REGION=eu-west-1  terraform plan
Error: No valid credential sources found for AWS Provider.
    Please see https://terraform.io/docs/providers/aws/index.html for more information on
    providing credentials for the AWS Provider

  on <empty> line 0:
  (source code not available)

Releasing state lock. This may take a few moments...

Testing also with terraform Terraform v0.14.8 and following this guidelines :

joey1089 commented 1 year ago

I came across this post for solutions but I found a solutions that seems to helped me out. so just wanted to sharing it here. Error: configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: operation error STS: GetCallerIdentity, https response error StatusCode: 403 . Similar error:- This is pertaining to terraform cloud aws setup. Solution: run echo "export AWS_REGION=us-east-1" | tee -a ~/.bash_profile export AWS_REGION=us-east-1 and run below cmd to check if its set aws sts get-caller-identity for more refer this article : https://aws-quickstart.github.io/workshop-terraform-modules/40_setup_cloud9_ide/41_setup_creds_in_c9.html#:~:text=AWS%20Terraform%20Workshop%20%3E%20Setup%20AWS%20Cloud9%20%3E,that%20you%20downloaded%20in%20the%20Getting%20Started%20Section.