no valid credential sources for Terraform AWS Provider found

Viewed 4806

I am using shared_cred_file for aws provider. With aws provider version 3.63 for example, terraform plan works good.

When I use aws provider 4.0 it prompts me to use apply changed setting for shared_credentials_files. After the changes, there is no error, but the second error remains

what could be the problem?

Warning: Argument is deprecated
│
│   with provider[“registry.terraform.io/hashicorp/aws”],
│   on main.tf line 15, in provider “aws”:
│   15:   shared_credentials_file = “~/.aws/credentials”
│
│ Use shared_credentials_files instead.
│
│ (and one more similar warning elsewhere)
╵
╷
│ 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: no EC2 IMDS role found, operation error ec2imds: GetMetadata, canceled, context deadline exceeded
│
│
│   with provider[“registry.terraform.io/hashicorp/aws”],
│   on main.tf line 13, in provider “aws”:
│   13: provider “aws” {
│
///////////////////////////////
// Infrastructure init
terraform {
  backend "s3" {
    bucket                  = "monitoring-********-infrastructure"
    key                     = "tfstates/********-non-prod-rds-info.tfstate"
    profile                 = "test-prof"
    region                  = "eu-west-2"
    shared_credentials_file = "~/.aws/credentials"
  }
}

    provider "aws" {
      profile                 = "test-prof"
      shared_credentials_files = ["~/.aws/credentials"]
      region                  = "eu-west-2"
    }
    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: no EC2 IMDS role found, operation error ec2imds: GetMetadata, canceled, context deadline exceeded
        │
        │
        │   with provider["registry.terraform.io/hashicorp/aws"],
        │   on main.tf line 13, in provider "aws":
        │   13: provider "aws" {

cat config

[test-prof]
output = json
region = eu-west-2

cat credentials

[test-prof]
aws_access_key_id = ****************
aws_secret_access_key = ******************
3 Answers

By latest Terraform documentation this is how it will work,

provider "aws" {
  region                    = "us-east-1"
  shared_credentials_files  = ["C:/Users/tf_user/.aws/credentials"]
  profile                   = "customprofile"
}

I had the same issue this thing works for me.

Changing

provider "aws" {
  shared_credentials_file = "$HOME/.aws/credentials"
  profile                 = "default"
  region                  = "us-east-1"
}

to

provider "aws" {
  shared_credentials_file = "/Users/me/.aws/credentials"
  profile                 = "default"
  region                  = "us-east-1"
}

worked for me.

We stumbled with this issue in our pipelines after migration AWS Provider from version 3 -> 4.

So, for anyone using Azure DevOps or any other CI tools, the fix should be as easy as adding a new step in the pipeline and creating the shared credentials file:

mkdir $HOME/.aws

echo [default] >> $HOME/.aws/credentials
echo aws_access_key_id = ${AWS_ACCESS_KEY_ID} >> $HOME/.aws/credentials
echo aws_secret_access_key = ${AWS_SECRET_ACCESS_KEY} >> $HOME/.aws/credentials

AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY should be defined as a var or secrets in your pipeline.

azure pipeline

Related