我希望我的 Amazon Elastic Container Service (Amazon ECS) 任务在另一个账户中担任 AWS Identity and Access Management (IAM) 角色。
简短描述
您可以设置 Amazon ECS 任务,在另一个账户中代入 IAM 角色以执行以下操作:
- 访问资源,例如 Amazon Simple Storage Service (Amazon S3) 存储桶
- 通过 API 调用执行任务,例如描述资源以及启动或停止实例。
要允许您的 Amazon ECS 任务代入另一个 AWS 账户中的 IAM 角色,请完成以下步骤:
- 在源账户中配置 IAM 角色。
- 修改目标账户的 IAM 角色的信任策略,以允许源账户的 IAM 角色代入目标账户的 IAM 角色。
- 在源账户中创建任务定义,并将步骤 1 中创建的 IAM 角色定义为 Amazon ECS 任务角色。
解决方法
本文中使用的示例引用了两个不同的 AWS 账户:
- 托管 Amazon ECS 任务的源账户: 1111222233334444
- 包含 Amazon ECS 任务代入的 IAM 角色(例如 destination-account-role)的目标账户: 5555666677778888
在源账户中配置 IAM 角色
将以下策略声明添加到您的 Amazon ECS 任务角色中,以允许该角色代入目标账户中的 IAM 角色:
{
"Version": "2012-10-17",
"Statement": {
"Effect": "Allow",
"Action": "sts:AssumeRole",
"Resource": "arn:aws:iam::5555666677778888:role/destination-account-role"
}
}
注意: 将 5555666677778888 替换为您的任务需要代入的跨账户角色的账户 ID。将 destination-account-role 替换为代入角色的名称。
修改目标账户中 IAM 角色的信任策略
将以下策略声明添加到目标账户中的跨账户 IAM 角色 (destination-account-role) 的信任策略中:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::1111222233334444:role/my-ECS-task-role"
},
"Action": "sts:AssumeRole"
}]
}
注意: 将 1111222233334444 替换为存在 ECS 任务 IAM 角色的源账户的账户 ID。将 my-ECS-task-role 替换为您的 ECS IAM 任务角色的名称。
创建任务定义
创建类似于以下内容的任务定义文件:
{
"containerDefinitions": [
{
"name": "test",
"image": "your-test-image",
"cpu": 100,
"memory": 200,
"essential": true
}
],
"family": "verify-assume-cross-account-role",
"taskRoleArn": "arn:aws:iam::1111222233334444:role/my-ECS-task-role"
}
注意: 对于 taskRoleArn,使用源账户 IAM 角色的 ARN。
要注册任务定义,请在 example-task-def.json 文件中运行以下命令:
aws ecs register-task-definition --cli-input-json file://example-task-def.json
注意: 如果在运行 AWS 命令行界面 (AWS CLI) 命令时收到错误,请参阅 Troubleshoot AWS CLI errors(AWS CLI 错误故障排除)。此外,请确认您使用的是最新版本的 AWS CLI。
完成上述步骤后,使用 AWS CLI 运行独立任务,在目标账户上代入 IAM 角色。或者,您可以使用配置文件中的 credential_source 设置。指定 AWS CLI 可以在哪里找到代入连接到 ECS 容器的 IAM 角色的凭证。有关详细信息,请参阅 Assume role credential provider(代入角色凭证提供者)。
验证任务中的容器是否可以代入目标账户中的 IAM 角色并访问资源
-
然后,使用您创建的任务定义来运行任务:
如果您在 Amazon Elastic Compute Cloud (Amazon EC2) 上运行任务,请使用 docker exec 命令来执行测试。
如果您在 AWS Fargate 上运行任务,那么使用 ECS Exec 来执行测试。
-
配置 AWS CLI 配置文件,然后验证该任务是否在目标账户中代入 IAM 角色:
Using the ECS exec command to access the container
$ aws ecs execute-command --cluster example-cluster --task example-taskID --container test --interactive --command "/bin/bash"
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
Starting session with SessionId: ecs-execute-command-064a40c5149cecc32
# Create AWS CLI config file
bash-4.2# mkdir /root/.aws
bash-4.2# cat < /root/.aws/config
[profile cross-account]
role_arn = arn:aws:iam::5555666677778888:role/destination-account-role
credential_source = EcsContainer
EOF
# Check the current task IAM role
bash-4.2# aws sts get-caller-identity
{
"UserId": "AROA4SHE6JAGEAYNUH6ST:8ee54a7f5c474a3f93ee28474486402f",
"Account": "1111222233334444",
"Arn": "arn:aws:sts::1111222233334444:assumed-role/my-ECS-task-role/8ee54a7f5c474a3f93ee28474486402f"
}
# Assume the cross-account IAM role
bash-4.2# aws sts get-caller-identity --profile cross-account
{
"UserId": "AROA3A44JRHY6FFSMMJKN:botocore-session-1647426859",
"Account": "5555666677778888",
"Arn": "arn:aws:sts::5555666677778888:assumed-role/destination-account-role/botocore-session-1647426859"
}
# Verify that you can list the resources in cross-account in the task
bash-4.2# aws ecs list-clusters --profile cross-account
{
"clusterArns": [
"arn:aws:ecs:us-east-1:5555666677778888:cluster/default"
]
}
如果您的输出与前面的示例类似,则账户 1111222233334444 中的 ECS 任务可以代入账户 5555666677778888 中的 IAM 角色。ECS 任务可以代入 IAM 角色来列出 ECS 集群资源。
相关信息
Amazon ECS 任务 IAM 角色
IAM 教程: 使用 IAM 角色委托跨 AWS 账户的访问权限
What is covered in the AWS SDKs and Tools Reference Guide(AWS SDK 和工具参考指南涵盖的内容)
在 AWS CLI 中使用 IAM 角色