3
votes

[----------------------- UPDATE --------------------------]

I have tried a tutorial to integrate terraform with s3 now. The S3 bucket is created and I have created an IAM user, and I am using its Access key and secret key. Nonetheless I keep getting errors regarding the providers after terraform init:

backend.tf

terraform {
  required_version = ">=0.12.0"
  backend "s3" {
    region  = "us-east-1"
    key     = "terraform.tfstate"
    profile = "tu"
    bucket  = "terraformstatebucket3107"
  }
}

config file in .aws folder

[tu]
region = us-east-1
output = json

credentials file in .aws folder

[tu]
aws_access_key_id = AKIA*****************
aws_secret_access_key = nn3M1*****************

Error:

Initializing the backend...

Error: error configuring S3 Backend: no valid credential sources for S3 Backend found.

Please see https://www.terraform.io/docs/backends/types/s3.html
for more information about providing credentials.

Error: NoCredentialProviders: no valid providers in chain. Deprecated.
    For verbose messaging see aws.Config.CredentialsChainVerboseErrors
3
Did you run terraform init prior to running apply? - Marko E
@MarkoE Yes, sure did - ack31
Are your AWS access key and secret access key by any chance in all caps in the credentials file? - Marko E
I would agree that if the error message persists and you have set the creds in various locations, then they are probably malformed somehow. - Matt Schuchard
As something else to try, drop the profile = "default" line - lxop

3 Answers

1
votes

Do run terraform init you have to add -backend-config options for your credentials (aws keys).

1
votes

So, I have tried every solution that was suggested here, but unfortunately none of them solved my issue. After some digging, I found a solution that worked for me. That was executing the terraform init command with the -backend-config option like this:

terraform init -backend-config="access_key=<your access key>" -backend-config="secret_key=<your secret key>"

This is the question where I found this solution: Error while configuring Terraform S3 Backend

1
votes

There are couple of things you need to check.

  1. There should be a file under .aws folder with name credentials and content of that file should be having access key and secret key.

    [tu]
    aws_access_key_id = ***************
    aws_secret_access_key = ************************
    
  2. If the above file is present with correct keys, and still same error is coming then I will suggest you should mention access key and secret key in your provider block like this

     provider "aws" {
       access_key = var.aws_access_key
       secret_key = var.aws_secret_key
       region = var.region
     }
    

And store the above variables values in .tfvars file

aws_access_key ="****your access key****"
aws_secret_key = "***your secret key****"

Hope with these settings it should work and even after this issue persist, then I see in your .aws/config you have mentioned mfa arn.

If Multi-factor Authentication is enabled then try adding mfa session token in backend.tf file as well along with provider block that I have mentioned above

terraform {
  required_version = ">=0.12.0"
  backend "s3" {
    region  = "us-east-1"
    key     = "terraform.tfstate"
    bucket  = "terraformstatebucket3107"
    token   = "*****Your MFA SessionToken*****"
  }
}

To get the MFA SessionToken please check here