Get Hands-on with Amazon EKS - Workshop Event Series
Whether you're taking your first steps with Kubernetes or you're an experienced practitioner looking to sharpen your skills, our Amazon EKS workshop series delivers practical, real-world experience that moves you forward. Learn directly from AWS solutions architects and EKS specialists through hands-on sessions designed to build your confidence with Kubernetes. Register now and start building with Amazon EKS!
如何將我的 Amazon ECS 任務設定為在另一個 AWS 帳戶中承擔 IAM 角色?
我希望我的 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 Command Line Interface (AWS CLI) 命令時收到錯誤,請參閱對 AWS CLI 錯誤進行疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
完成上述步驟後,使用 AWS CLI 執行獨立任務,以在目的地帳戶上承擔 IAM 角色。或者,您可以使組態檔中的 credential_source 設定。指定 AWS CLI 可在哪裡尋找憑證以承擔附加至 ECS 容器的 IAM 角色。如需詳細資訊,請參閱承擔角色憑證供應商。
確認任務中的容器可以在目的地帳戶中承擔 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 <<EOF > /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 叢集資源。
相關資訊
- 語言
- 中文 (繁體)

相關內容
AWS 官方已更新 1 年前