简体   繁体   English

如何让 Terraform 读取 AWS 凭证文件?

[英]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:我正在尝试使用 terraform 创建一个 AWS S3 存储桶,这是我的代码:

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:我已经使用记事本手动创建了“凭据”文件,还使用 ​​Powershell 删除了“.txt”扩展名,并将该文件存储在C:\Users\terraform\.aws中,该文件如下所示:

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

But when I try to run terraform plan , I get an error which says但是当我尝试运行terraform plan时,我收到一个错误,上面写着

ERROR: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found错误:配置 Terraform AWS 提供程序时出错:找不到 Terraform AWS 提供程序的有效凭证源

Then, I also tried to create that "Credentials" file by installing AWS CLI, I ran the command然后,我还尝试通过安装 AWS CLI 创建该“凭据”文件,我运行了命令

aws configure --profile terraform

where terraform was my username. terraform是我的用户名。 So, it asked me to enter aws_access_key_id and aws_secret_access_key .因此,它要求我输入aws_access_key_idaws_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:输入所有凭据后,我运行了命令terraform init ,该命令运行成功,但是当我运行terraform plan时,它再次显示错误,上面写着:

ERROR: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found错误:配置 Terraform AWS 提供程序时出错:找不到 Terraform AWS 提供程序的有效凭证源

When you create 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当您不想手动放置共享文件时

Need to be in this path %USERPROFILE%.aws\credentials需要在此路径%USERPROFILE%.aws\credentials

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

If you want to put your credentials in a tf file如果你想把你的凭据放在一个 tf 文件中

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

I've spent quite a bit of time trying to figure out how to get Terraform to read ~/.aws/credentials.我花了很多时间试图弄清楚如何让 Terraform 读取 ~/.aws/credentials。 The only option that worked for me was specifying AWS_PROFILE environment var to point it to the specific section of the credentials file.对我有用的唯一选项是指定 AWS_PROFILE 环境变量以将其指向凭证文件的特定部分。

AWS_PROFILE=prod terraform plan

or或者

export AWS_PROFILE=prod 
terraform plan

The fact that the shared_credentials_file and/or the profile options in the provider section get ignored looks like a bug to me.提供程序部分中的shared_credentials_file和/或配置文件选项被忽略的事实对我来说似乎是一个错误。

The path where you are storing the credentials file is wrong.您存储凭据文件的路径错误。

C:\Users\your-username\.aws C:\Users\你的用户名\.aws

You can add these below files in the above location.您可以在上述位置添加以下这些文件。

credentials证书

[default]
aws_access_key_id = your access key
aws_secret_access_key = your secret key

config配置

[default]
region=ap-south-1

And you don't need to configure any thing into terraform or python if you're using boto3.如果您使用的是 boto3,则无需将任何东西配置到 terraform 或 python 中。 Terraform and boto3 will automatically find the desired credentials file. Terraform 和 boto3 将自动找到所需的凭据文件。

You have to set up a custom section in your credentials file with the command您必须使用命令在凭据文件中设置自定义部分

aws configure --profile=prod 

in order to use env variable like this.为了像这样使用环境变量。

when you have AWS cli already installed in local then go to config file path: %USERPROFILE%\.aws\credentials Update Credentials as below:当您已经在本地安装了 AWS cli 时,请转到配置文件路径: %USERPROFILE%\.aws\credentials更新凭证如下:

[default]
aws_access_key_id = "xxxxx"
aws_secret_access_key = "xxxxx"
region= us-east-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM