AWS codebuild 列出其他帐户的 s3 存储桶

AWS codebuild to list out s3 buckets of other account

我的代码构建位于 账户 A 上,s3 存储桶位于 账户 B 上。我尝试在 账户 B 上设置受信任的 IAM STS 角色并在 账户 A 上设置策略以包含 账户 B IAM 角色,将此策略附加到我的代码构建服务角色。但是,我的代码构建仍然显示了 s3 上的存储桶。我在这里做错了或配置错了吗?

对账户 B 具有信任关系的角色

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::Account:root"
      },
      "Action": "sts:AssumeRole",
      "Condition": {}
    }
  ]

账户 A 的政策

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::Account B:role/testcli"
        }
    ]
}

CodeBuild BuildSpec.yml

version: 0.2

env:
  variables:
    TF_VERSION: "0.12.28"

phases:
  install:
    commands:
#       install required binary
      - echo test
  pre_build:
    commands:
      - echo print s3 buckets
      - aws s3 ls

  post_build:
    commands:
      - echo test1

假设您的 CodeBuild (CB) 具有 sts:AssumeRole 的权限,在您的 buildspec.yml 中,您必须 明确地 承担 Acc B 中的角色。

两种方法可以做到这一点。

  • 在您的 buildspec.yml 中“手动”调用 assume-role。该调用将 return 一组临时凭证。获得的凭证随后可用于从您的 CB 在 Acc B 中执行 AWS CLI 命令。

  • 在您的 CB 容器中设置 AWS CLI 凭证文件 here or here 以承担角色。

在这两种情况下,CB service-role 需要 sts:AssumeRole 权限。