Search code examples
amazon-web-servicesamazon-s3terraform-provider-awsaws-credentialsterraform-aws-modules

How to make Terraform to read AWS Credentials file?


I am trying to create an AWS S3 bucket using terraform and this is my code:

provider "aws" {
  profile = "default"
  region  = "ap-south-1"
}

resource "aws_s3_bucket" "first_tf" {
  bucket = "svk-pl-2909202022"
  acl    = "private"
}

I have manually created the "Credentials" file using Notepad and also removed the ".txt" extension using Powershell and stored that file in C:\Users\terraform\.aws, and that file is like this:

[default]
aws_access_key_id=**************
aws_secret_access_key=************

But when I try to run terraform plan, I get an error which says

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

Then, I also tried to create that "Credentials" file by installing AWS CLI, I ran the command

aws configure --profile terraform

where terraform was my username. So, it asked me to enter aws_access_key_id and aws_secret_access_key. and after entering all the credentials, I ran the command terraform init, which ran successfully but when I ran terraform plan, it shows the error again which says:

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


Solution

  • When you create a profile manually

    provider "aws" {
      region                  = "your region"
      shared_credentials_file = "path_file_credentials like C:\Users\terraform\.aws\credentials"
      profile                 = "profile_name"
    }
    

    When you don't want to put your shared file manually

    That needs to be in this path %USERPROFILE%.aws\credentials

    provider "aws" {
      region                  = "your region"
      profile                 = "profile_name"
    }
    

    If you want to put your credentials in a tf file

    provider "aws" {
      region     = "us-west-2"
      access_key = "my-access-key"
      secret_key = "my-secret-key"
    }