【问题标题】:AWS Codepipeline with a Codecommit targetsource repository from another account具有来自另一个账户的 Codecommit 目标源存储库的 AWS Codepipeline
【发布时间】:2017-12-12 00:22:32
【问题描述】:

是否可以在另一个账户中创建具有 CodeCommit 存储库的目标源的代码管道?

【问题讨论】:

  • 嗨,你找到问题的答案了吗?
  • 没有。搜索继续。下面的答案列出了我在提问时引用的文档。不确定对方是否理解我的问题。

标签: amazon-web-services aws-codepipeline aws-codecommit


【解决方案1】:

您可以使用管道和另一个帐户中的 codecommit 存储库来部署资源。

假设您的代码提交存储库所在的帐户 A 和代码管道所在的帐户 B。

在账户 B 中配置以下内容:

  1. 您需要创建自定义 KMS 密钥,因为 AWS 默认密钥没有关联的密钥策略。如果您在创建 CMK 方面需要帮助,可以使用 Create a Pipeline in CodePipeline That Uses Resources from Another AWS Account。将 Codepipeline 服务角色添加到 KMS 密钥策略以允许 codepipeline 使用它。

  2. 用于从跨账户接收事件的事件总线转到 CloudWatch → 事件部分下的事件总线 → 添加权限 → 输入 DEV AWS 账户 ID → 添加。更多详情,请查看Creating an Event Bus

  3. 将以下策略添加到 S3 管道工件存储:

     { 
      “Version”: “2012–10–17”, 
      “Id”: “PolicyForKMSAccess”, 
      “Statement”: [ 
          { “Sid”: “AllowAccessFromAAccount”, 
            “Effect”: “Allow”, 
             “Principal”: { “AWS”: “arn:aws:iam::ACCOUNT_A_ID:root” }, 
             “Action”: [ “s3:Get*”, “s3:Put*”, "s3:ListBucket ], 
             “Resource”: “arn:aws:s3:::NAME-OF-THE-BUCKET/*” } 
           ] 
        }
    
  4. 编辑管道 IAM 角色以承担账户 A 的角色,如下所示:

        { 
           “Version”:“2012–10–17”,
           “Statement”:{ 
              “Effect”:“Allow”,
              “Action”:“sts:AssumeRole”,
              “Resource”:[ 
                 “arn:aws:iam::ACCOUNT_A_ID:role/*
              ]
           }
        }
    
  5. 创建 CloudWatch 事件规则以触发账户 A 中 CodeCommit 的主分支上的管道。添加 CodePipeline 的 ARN 作为此规则的目标。

现在,在帐户 A 中执行以下操作:

使用 3 个策略创建一个跨账户 IAM 角色。 a) AWSCodeCommitFullAccess

b) 承担账户 B 角色的内联策略如下:

    { 
       “Version”:“2012–10–17”,
       “Statement”:[ 
          { 
             “Effect”:“Allow”,
             “Principal”:{ 
                “AWS”:“arn:aws:iam::ACCOUNT_B_ID:root”
             },
             “Action”:“sts:AssumeRole”
          }
       ]
    }

c)KMS、CodeCommit 和 S3 访问的内联策略:

    { 
       “Version”:“2012–10–17”,
       “Statement”:[ 
          { 
             “Effect”:“Allow”,
             “Action”:[ 
                “s3:Get*”,
                “s3:Put*”,
                “codecommit:*”
             ],
             “Resource”:[ 
                “arn:aws:s3:::YOUR_BUCKET_NAME_IN_B_FOR_CODE_PIPELINE_ARTIFACTS/”
             ]
          },
          { 
             “Effect”:“Allow”,
             “Action”:[ 
                “kms:*" ], 
                “Resource”: [ “arn:aws:kms:YOUR_KMS_ARN_FROM_B_ACCOUNT” ] } ] }

2。按照@Eran Medan 的建议更新您的管道。

更多详情请访问AWS CodePipeline with a Cross-Account CodeCommit Repository

另外,请注意,我授予的权限比 codecommit:* 和 kms:* 所需的权限多得多,您可以根据需要更改它们。

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我只需要这样做,我会解释这个过程。

    账户 C 是您的 CodeCommit 存储库的账户。 帐户 P 是您的 CodePipeline... 管道的帐户。

    在帐户 P 中:

    1. 创建 AWS KMS 加密密钥并添加具有访问权限的账户 C(先决条件步骤中的指南 here)。您还需要添加 CodePipeline 角色,如果您有 CodeBuild 和 CodeDeploy 步骤,也需要添加这些角色。

    2. 在您的 CodePipeline 工件 S3 存储桶中,您需要添加账户 C 访问权限。转到存储桶策略并添加:

    {
        "Sid": "",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::ACCOUNTC_ID:root"
        },
        "Action": [
            "s3:Get*",
            "s3:Put*"
        ],
        "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
    },
    {
        "Sid": "",
        "Effect": "Allow",
        "Principal": {
            "AWS": "arn:aws:iam::ACCOUNTC_ID:root"
        },
        "Action": "s3:ListBucket",
        "Resource": "arn:aws:s3:::YOUR_BUCKET_NAME"
    }
    

    ACCOUNTC_ID 更改为账户C 的账户ID,并将YOUR_BUCKET_NAME 更改为CodePipeline 工件S3 存储桶名称。

    1. 向您的 CodePipeline 服务角色添加策略,以便您可以访问账户 C 和 CodeCommit 存储库:
    {
       "Version": "2012-10-17",
       "Statement": {
           "Effect": "Allow",
           "Action": "sts:AssumeRole",
           "Resource": [
               "arn:aws:iam::ACCOUNTC_ID:role/*"
           ]
       }
    }
    

    同样,将ACCOUNTC_ID 更改为账户 C 的账户 ID。

    在帐户 C 中:

    1. 创建允许账户 P 访问 CodeCommit 资源和 KMS 密钥的 IAM 策略,以便它可以使用与您的 CodePipeline 其余部分相同的密钥对其进行加密:
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "s3:GetObject*",
                    "s3:PutObject",
                    "s3:PutObjectAcl",
                    "codecommit:ListBranches",
                    "codecommit:ListRepositories"
                ],
                "Resource": [
                    "arn:aws:s3:::YOUR_BUCKET_NAME_IN_ACCOUNTP_FOR_CODE_PIPELINE/*"
                ]
            },
            {
                "Effect": "Allow",
                "Action": [
                    "kms:DescribeKey",
                    "kms:GenerateDataKey*",
                    "kms:Encrypt",
                    "kms:ReEncrypt*",
                    "kms:Decrypt"
                ],
                "Resource": [
                    "arn:aws:kms:YOUR_KMS_ARN"
                ]
            }
        ]
    }
    

    替换上述策略中的存储桶名称和 KMS ARN。将策略保存为 CrossAccountPipelinePolicy 之类的内容。

    1. 为跨账户访问创建角色并附加上述策略以及 AWSCodeCommitFullAccess 策略。确保将 Trusted entity 作为 Account P 的帐户 ID。

    在 AWS CLI 中 您无法在控制台中执行此操作,因此您必须使用 AWS CLI。这将让您在 AccountP 中的 CodePipeline 承担 Source 步骤中的角色并将其转储到 S3 存储桶中以供您接下来的所有步骤使用。

    aws codepipeline get-pipeline --name NameOfPipeline > pipeline.json

    修改管道 json 使其看起来有点像这样并替换您需要的位:

    "pipeline": {
            "name": "YOUR_PIPELINE_NAME",
            "roleArn": "arn:aws:iam::AccountP_ID:role/ROLE_NAME_FOR_CODE_PIPELINE",
            "artifactStore": {
                "type": "S3",
                "location": "YOUR_BUCKET_NAME",
                "encryptionKey": {
                  "id": "arn:aws:kms:YOUR_KMS_KEY_ARN",
                  "type": "KMS"
                }
            },
            "stages": [
                {
                    "name": "Source",
                    "actions": [
                        {
                            "name": "Source",
                            "actionTypeId": {
                                "category": "Source",
                                "owner": "AWS",
                                "provider": "CodeCommit",
                                "version": "1"
                            },
                            "runOrder": 1,
                            "roleArn": "arn:aws:iam::AccountC_ID:role/ROLE_NAME_WITH_CROSS_ACCOUNT_POLICY",
                            "configuration": {
                                "BranchName": "master",
                                "PollForSourceChanges": "false",
                                "RepositoryName": "YOURREPOSITORYNAME"
                            },
                            "outputArtifacts": [
                                {
                                    "name": "MyApp"
                                }
                            ],
                            "inputArtifacts": []
                        }
                    ]
                },
    

    aws codepipeline update-pipeline --cli-input-json file://pipeline.json更新管道

    通过运行管道验证它是否有效。

    【讨论】:

    • 谢谢。通过参考您的回答,我能够让我们的多帐户 CodePipeline 正常工作。
    • 我尝试了这个但是得到了错误An error occurred (AccessDeniedException) when calling the UpdatePipeline operation: Cross-account pass role is not allowed.你能帮忙吗。
    • 我试试这个,但是当我运行管道时出错The service role or action role doesn’t have the permissions required to access the Amazon S3 bucket named codepipeline-********. Update the IAM role permissions, and then try again. Error: Amazon S3:AccessDenied:Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 7DF5A64758465G6V; S3 Extended Request ID: n/tlbrqIZqy10Xp1V2bSDFRg9dTGf1MwAZpE0AD2fIP6pnCfoSf3QI02v6NBQInIVR3aJQdXIMk=; Proxy: null)你能帮忙吗?
    • @BhatasanaPrashant 您可以尝试“AWS_PROFILE=prodaccess aws codepipeline update-pipeline --cli-input-json file://pipeline.json”而不是“aws codepipeline update-pipeline --cli-input -json file://pipeline.json --profile prodaccess" 如果您使用的是配置文件语法。来源:github.com/serverless/serverless/issues/3374
    【解决方案3】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      相关资源
      最近更新 更多