简体   繁体   English

未找到 Terraform AWS 凭证文件

[英]Terraform AWS credentials file not found

In reading the docs over at Terraform it says there are 3 options for finding AWS credientials:在阅读 Terraform 上的文档时,它说有 3 个选项可以找到 AWS 凭证:

  1. Static Credentials( embedded in the source file )静态凭据(嵌入在源文件中)
  2. Environment variables.环境变量。
  3. From the AWS credentials file从 AWS 凭证文件

I am trying to have my setup just use the credential file.我试图让我的设置只使用凭证文件。 I've checked that the environment variables are cleared and I have left the relevant variables in Terraform blank.我已经检查过环境变量是否已清除,并将 Terraform 中的相关变量留空。

When I do this and run 'Terraform Plan' I get the error:当我这样做并运行“Terraform Plan”时,我收到错误消息:

No Valid credential sources found for AWS Provider.未找到适用于 AWS 提供商的有效凭证源。

I've even tried adding the location of my credentials file into my provider block and that didn't help either:我什至尝试将我的凭据文件的位置添加到我的 provider 块中,但这也无济于事:

provider "aws" {
    region  = "${var.region}"
    profile = "${var.profile}"
    shared_credentials_file = "/Users/david/.aws/credentials"
    profile = "testing"
}

Is there something I'm missing to get Terraform to read this file and not require environment variables?我是否缺少让 Terraform 读取此文件而不需要环境变量的东西?

I tested with Terraform v0.6.15 and its working fine.我使用Terraform v0.6.15进行了测试,并且工作正常。

Issue must be with the profile .问题必须与profile Check the following.检查以下内容。

1. Remove 2 profile tags from your provider. 1. 从您的提供商中删除 2 个profile标签。

provider "aws" {
  region  = "${var.region}"
  shared_credentials_file = "/Users/david/.aws/credentials"
  profile = "testing"
}

2. Make sure your credentials file /Users/david/.aws/credentials is in the below format, where testing is the profile you are specifying in provider "aws" 2. 确保您的凭证文件/Users/david/.aws/credentials采用以下格式,其中testing是您在provider "aws"中指定的profile

[testing]
aws_access_key_id = *****
aws_secret_access_key = *****

To get multiple profiles to work with Terraform make sure that you supply the要让多个配置文件与 Terraform 一起使用,请确保提供

aws_access_key_id 

piece to your profile declaration.一块到您的个人资料声明。 Each profile should look like this:每个配置文件应如下所示:

[profile_name]
aws_access_key=*****
aws_secret_access_key****
aws_access_key_id=*****

Technically you don't even need the aws_access_key as it seems the id version is what the underlying aws cli needs.从技术上讲,您甚至不需要 aws_access_key,因为 id 版本似乎是底层 aws cli 所需要的。 Maybe it was me, but that was never clear in the documents I read.也许是我,但在我阅读的文件中从来没有明确过这一点。

I just had this same problem with terraform aws provider (2.12.0) and this is how I solved it.我刚刚在 terraform aws provider (2.12.0) 上遇到了同样的问题,这就是我解决的方法。

In my case the provider couldn't handle that my default profile in $HOME/.aws/credentials did NOT have my access key and secret but it had a "source_profile" in it instead.在我的情况下,提供商无法处理$HOME/.aws/credentials默认配置文件没有我的访问密钥和机密,但它有一个“source_profile”。 It seems the terraform aws provider cannot handle this (yet this works for Java SDK and AWS CLI just fine since I've had this setup for awhile now).似乎 terraform aws 提供商无法处理这个问题(但这对 Java SDK 和 AWS CLI 很好用,因为我已经有一段时间了)。

Here is what I had that didn't work, notice the default profile has a role_arn and source_profile:这是我所拥有的不起作用的东西,请注意默认配置文件有一个 role_arn 和 source_profile:

[default]
role_arn = arn:aws:iam::<ACCT_ID>:role/readonly
source_profile = account
region = us-east-1

[other-profile]
role_arn = arn:aws:iam::<ACCT_ID>:role/other-role
source_profile = account
region = us-east-1

[account]
region = us-east-1
aws_access_key_id=****
aws_secret_access_key=****

I changed it to the following which resulted in the aws provider working for me.我将其更改为以下内容,这导致 aws 提供程序为我工作。 Notice I consolidated two profiles into the "default" profile:请注意,我将两个配置文件合并到“默认”配置文件中:

[other-profile]
role_arn = arn:aws:iam::<ACCT_ID>:role/other-role
source_profile = default
region = us-east-1

[default]
region = us-east-1
aws_access_key_id=****
aws_secret_access_key=****
role_arn = arn:aws:iam::<ACCT_ID>:role/readonly
source_profile = default

This seems to work fine for the AWS CLI (defaults to the readonly role and supports switching to "other-profile") as well as allowing terraform to read credentials correctly.这似乎适用于 AWS CLI(默认为只读角色并支持切换到“other-profile”)以及允许 terraform 正确读取凭证。

请为实例分配管理员角色,然后它将正常工作

(Terraform v0.14.2, macOS 11.0.1) (Terraform v0.14.2,macOS 11.0.1)

I needed to do:我需要做:

AWS_ACCESS_KEY_ID=... AWS_SECRET_ACCESS_KEY=... terraform plan

Which was strange to me, because my ~/.aws is in order, as are my .tf-s.这对我来说很奇怪,因为我的 ~/.aws 是有序的,我的 .tf-s 也是如此。 ¯_(ツ)_/¯ ¯_(ツ)_/¯

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

相关问题 如何让 Terraform 读取 AWS 凭证文件? - How to make Terraform to read AWS Credentials file? 找不到AWS凭证错误 - aws credentials not found error 没有AWS凭证的Terraform运行计划 - Terraform run plan without AWS credentials Terraform aws_instance 指定登录凭据 - Terraform aws_instance specify login credentials Terraform 抱怨 CICD 管道中缺少 AWS 凭证 - Terraform complains lack of AWS credentials in CICD Pipeline AWS CDK Python(未找到凭证) - AWS CDK Python (No Credentials Found) Terraform AWS | 错误:配置 Terraform AWS 提供商时出错:找不到 Terraform AWS 提供商的有效凭证源 - Terraform AWS | Error: error configuring Terraform AWS Provider: no valid credential sources for Terraform AWS Provider found 无法从 ~/.aws/config 和 ~/.aws/credentials 文件加载 AWS 凭证 - Unable to load AWS credentials from ~/.aws/config and ~/.aws/credentials file AWS Terraform:│ 错误:配置 Terraform AWS 提供商时出错:验证提供商凭证时出错:调用 sts:GetCallerIdentity 时出错: - AWS Terraform: │ Error: error configuring Terraform AWS Provider: error validating provider credentials: error calling sts:GetCallerIdentity: Terraform不接受来自vars / tfvars文件的AWS凭证 - Terraform not accepting AWS credentials from vars/tfvars files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM