【发布时间】:2020-02-23 15:21:31
【问题描述】:
我这几天正试图解开这个谜团,但没有任何乐趣。基本上,Terraform 无法承担角色并且失败:
Initializing the backend...
2019/10/28 09:13:09 [DEBUG] New state was assigned lineage "136dca1a-b46b-1e64-0ef2-efd6799b4ebc"
2019/10/28 09:13:09 [INFO] Setting AWS metadata API timeout to 100ms
2019/10/28 09:13:09 [INFO] Ignoring AWS metadata API endpoint at default location as it doesn't return any instance-id
2019/10/28 09:13:09 [INFO] AWS Auth provider used: "SharedCredentialsProvider"
2019/10/28 09:13:09 [INFO] Attempting to AssumeRole arn:aws:iam::72xxxxxxxxxx:role/terraform-admin-np (SessionName: "terra_cnp", ExternalId: "", Policy: "")
Error: The role "arn:aws:iam::72xxxxxxxxxx:role/terraform-admin-np" cannot be assumed.
There are a number of possible causes of this - the most common are:
* The credentials used in order to assume the role are invalid
* The credentials do not have appropriate permission to assume the role
* The role ARN is not valid
在 AWS 中:
我有角色:terraform-admin-np 和 2 个AWS 托管策略:AmazonS3FullAccess 和 AdministratorAccess,并与此建立信任关系:
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::72xxxxxxxxxx:root"
},
"Action": "sts:AssumeRole"
}
]
}
然后我有一个 用户 附有政策文件:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "TfFullAccessSts",
"Effect": "Allow",
"Action": [
"sts:AssumeRole",
"sts:DecodeAuthorizationMessage",
"sts:AssumeRoleWithSAML",
"sts:AssumeRoleWithWebIdentity"
],
"Resource": "*"
},
{
"Sid": "TfFullAccessAll",
"Effect": "Allow",
"Action": "*",
"Resource": [
"*",
"arn:aws:ec2:region:account:network-interface/*"
]
}
]
}
和一个 S3 存储桶:txxxxxxxxxxxxxxte 附有此政策文件:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "TFStateListBucket",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::72xxxxxxxxxx:root"
},
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::txxxxxxxxxxxxxxte"
},
{
"Sid": "TFStateGetPutObject",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::72xxxxxxxxxx:root"
},
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::txxxxxxxxxxxxxxte/*"
}
]
}
在 Terraform 中:
来自provider.tf的sn-p:
###---- Default Backend and Provider config values -----------###
terraform {
required_version = ">= 0.12"
backend "s3" {
encrypt = true
}
}
provider "aws" {
region = var.region
version = "~> 2.20"
profile = var.profile
assume_role {
role_arn = var.role_arn
session_name = var.session_name
}
}
来自tgw_cnp.tfvars 后端配置的片段:
## S3 backend config
key = "backend/tgw_cnp_state"
bucket = "txxxxxxxxxxxxxxte"
region = "us-east-2"
profile = "local-tgw"
role_arn = "arn:aws:iam::72xxxxxxxxxx:role/terraform-admin-np"
session_name = "terra_cnp"
然后这样运行:
TF_LOG=debug terraform init -backend-config=tgw_cnp.tfvars
有了这个,我可以毫无问题地使用 AWS CLI 担任角色:
# aws --profile local-tgw sts assume-role --role-arn "arn:aws:iam::72xxxxxxxxxx:role/terraform-admin-np" --role-session-name AWSCLI
{
"Credentials": {
"AccessKeyId": "AXXXXXXXXXXXXXXXXXXA",
"SecretAccessKey": "UixxxxxxxxxxxxxxxxxxxxxxxxxxxxMt",
"SessionToken": "FQoGZXIvYXdzEJb//////////wEaD......./5LFwNWf6riiNw9vtBQ==",
"Expiration": "2019-10-28T13:39:41Z"
},
"AssumedRoleUser": {
"AssumedRoleId": "AROA2P7ZON5TSWMOBQEBC:AWSCLI",
"Arn": "arn:aws:sts::72xxxxxxxxxx:assumed-role/terraform-admin-np/AWSCLI"
}
}
但是 terraform 因上述错误而失败。知道我做错了什么吗?
【问题讨论】:
-
您能否分享您曾经担任角色的 AWS CLI 命令(删除敏感数据后)?
-
确定@krishna_mee2004 - 我实际上已将输出添加到 OP。
-
看起来您在使用 CLI 时正在使用
default-tgw配置文件。但是在 Terraform 中,您提到了local-tgw。您可以尝试使用 local-tgw 配置文件的 CLI 并查看您是否可以担任该角色? -
那是我愚蠢的错字@krishna_mee2004 - 实际的个人资料名称既不是
local-tgw也不是defaut-tgw。命名上有点敏感,所以我只是试图掩盖它并在此期间犯了错误。我可以保证在 TF 和 CLI 中使用完全相同的配置文件。我修正了 OP 中的错字以尽量减少混乱。 -
您的设置在理想情况下应该没有任何问题,它对我有用。要检查的一件事是:您是否在 ~/.aws/credentials 文件中定义了配置文件
local-tgw的键?
标签: amazon-web-services amazon-s3 terraform assume-role