amazon-web-services - 为什么没有正确假设我在 aws 上的角色?
问题描述
在 AWS 上,我通过 terraform "My-role-ReadOnly" 在主账户上创建了一个角色,我试图从另一个账户承担这个角色,但如果我现在检查命令 "aws sts get-caller-identity" ,则假设不正确,我假设应该是其他身份?如果我错了,请纠正我。
resource "aws_iam_role" "My-role-ReadOnly" {
name = "My-role-ReadOnly"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::<my_not_main_account_id>:root"
},
"Action": "sts:AssumeRole",
"Condition": {}
}
]
}
EOF
}
data "aws_iam_policy" "ReadOnlyAccess" {
arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}
resource "aws_iam_policy_attachment" "test-attach" {
name = "test-attachment"
roles = [aws_iam_role.My-role-ReadOnly.name]
policy_arn = data.aws_iam_policy.ReadOnlyAccess.arn
}
H:\tf>aws sts assume-role --role-arn "arn:aws:iam::<main-account-id>:role/My-role-ReadOnly" --role-
session-name AWSCLI-Session
{
"AssumedRoleUser": {
"AssumedRoleId": "ARO:AWSCLI-Session",
"Arn": "arn:aws:sts::<main-account-id>:assumed-role/My-role-ReadOnly/AWSCLI-Session"
},
"Credentials": {
"SecretAccessKey": "/rV380jxxxxx",
"SessionToken": "FwoGZXxxxxx",
"Expiration": "2021-02-01T12:34:28Z",
"AccessKeyId": "ASxxxxxx"
}
}
C:\> setx AWS_ACCESS_KEY_ID ASxxxxxx
C:\> setx AWS_SECRET_ACCESS_KEY /rV380jxxxxx
C:\> setx AWS_DEFAULT_REGION eu-west-1
H:\tf>aws sts get-caller-identity
{
"Account": "<my_not_main_account_id>",
"UserId": "AIxxxxxxxx",
"Arn": "arn:aws:iam::<my_not_main_account_id>:user/terraform_user"
}
解决方案
aws sts assume-role
返回新角色的临时凭据,它不会更改当前环境,也不会更改您当前登录的身份/身份。
您需要将检索到的凭据导出到环境中。例如,您可以使用jq
:
credentials=$(aws sts assume-role --role-arn "$provider_arn" --role-session-name "$provider_session_name" | jq ".Credentials")
export AWS_ACCESS_KEY_ID=$(echo $credentials | jq -r ".AccessKeyId")
export AWS_SECRET_ACCESS_KEY=$(echo $credentials | jq -r ".SecretAccessKey")
export AWS_SESSION_TOKEN=$(echo $credentials | jq -r ".SessionToken")
(此语法取决于您的操作系统/外壳)
只有在导出凭据后才会aws sts get-caller-identity
返回新身份。
推荐阅读
- ios - BLE WroteCharacteristicValue 从不调用
- eclipse - Eclipse 清除调试外壳
- spring-boot - 在 AWS EC2(Route 53 + VPC)上使用 Spring Boot 微服务配置 Netflix Eureka
- sorting - j 在选择排序中的价值在哪里?基本 Q,Python 3
- javascript - 如何使用 javascript 在 servlet 中禁用下拉选项
- google-apps-script - 无法使用应用程序脚本 UrlFetchApp 获取共享的谷歌驱动器链接(任何人)
- java - 如何忽略以 // 使用扫描程序类和分隔符开头的整行文本
- java - 如何从 Raspberry Pi 我的 firebase 项目连接 Java 类?
- ios - 通过 iOS 的 JSON 请求和在 REST Web 中的请求编码?
- matlab - 如何从 .mhd 图像中读取 DimeSize?