如何將我的 Amazon ECS 任務設定為在另一個 AWS 帳戶中擔任 IAM 角色?

3 分的閱讀內容
0

我想要設定自己的 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 角色,請執行以下操作:

  1. 在來源帳戶中設定 IAM 角色。
  2. 修改目標帳戶 IAM 角色的信任政策,以允許來源帳戶的 IAM 角色擔任目標帳戶中的 IAM 角色。
  3. 在來源帳戶中建立任務定義,將在步驟 1 中建立的 IAM 角色定義為 Amazon ECS 任務角色

解決方案

本文中使用的範例引用了兩個不同的 AWS 帳戶:

  • 託管 Amazon ECS 任務的來源帳戶 (範例:1111222233334444)
  • 包含 Amazon ECS 任務擔任的 IAM 角色 (範例:destination-account-role) 的目標帳戶 (範例:5555666677778888)

在來源帳戶中設定 IAM 角色

按照新增和刪除 IAM 身分許可中的説明,將以下政策聲明新增至您的 Amazon ECS 任務角色 (範例:my-ECS-task-role)。此動作可讓 ECS 任務角色在目標帳戶中擔任 IAM 角色。

注意:

  • 5555666677778888 取代為您的任務需要擔任的跨帳戶角色的帳户 ID。
  • destination-account-role 取代為所擔任角色的名稱。
{
  "Version": "2012-10-17",
  "Statement": {
    "Effect": "Allow",
    "Action": "sts:AssumeRole",
    "Resource": "arn:aws:iam::5555666677778888:role/destination-account-role"
  }
}

修改目標帳戶中 IAM 角色的信任政策

按照修改角色信任政策 (主控台) 中的説明,將以下政策聲明新增至目標帳戶中的跨帳戶 IAM 角色 (destination-account-role) 信任政策。

注意:

  • 1111222233334444 取代為 ECS 任務 IAM 角色所在的來源帳戶的帳戶 ID。
  • my-ECS-task-role 取代為 ECS IAM 任務角色的名稱。
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": {
      "AWS": "arn:aws:iam::1111222233334444:role/my-ECS-task-role"
    },
    "Action": "sts:AssumeRole"
  }]
}

建立任務定義

建立類似於以下內容的任務定義文件 (example-task-def.json),並將來源帳戶的 IAM 角色 (my-ECS-task-role) 的 ARN 用於 taskRoleArn

{
  "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"
}

執行以下命令,使用 example-task-def.json 檔案註冊任務定義:

aws ecs register-task-definition —cli-input-json file://example-task-def.json

**注意:**如果您在執行 AWS CLI 命令時收到錯誤,請確保您使用的是最新版 AWS CLI

完成上述步驟後,您可以使用 AWS Command Line Interface (AWS CLI) 執行獨立任務以在目標帳戶上擔任 IAM 角色。或者,也可以使用 AWS CLI 組態檔案中的 credential_source 設定來指定 AWS CLI 可在何處找到憑證以擔任連接至 ECS 容器的 IAM 角色。藉助此設定,任務可以擔任角色,而無需匯出新憑證。有關詳細資訊,請參閲擔任角色憑證

驗證任務中的容器是否可以在目標帳戶中擔任 IAM 角色並存取資源

1.    使用您建立的任務定義執行任務。

  • 如果您正在 Amazon Elastic Compute Cloud (Amazon EC2) 上執行任務,請使用 docker exec 命令進入容器以執行測試。
  • 如果您在 AWS Fargate 上執行任務,請使用 ECS Exec 功能進入容器執行測試。

2.    設定 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 任務可以在帳戶 55555566667777778888 中擔任 IAM 角色來列出 ECS 叢集資源。


相關資訊

用於任務的 IAM 角色

IAM 教學:使用 IAM 角色將存取權委派至 AWS 帳戶

使用共用組態和憑證檔案的 AWS 軟體開發套件與工具

憑證檔案設定

AWS 官方
AWS 官方已更新 2 年前