跨账户ECS部署

时间:2019-09-24 21:05:04

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

我正在尝试使用CodePipeline将映像从一个帐户(AccountA)的ECR部署到另一个帐户(AccountB)的ECS群集。在部署阶段出现权限相关错误。

这是我在AccountA中的管道角色:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:GetObject",
                "s3:GetObjectVersion",
                "s3:GetBucketVersioning"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::<bucketname>/*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "ecr:GetAuthorizationToken",
                "ecr:BatchCheckLayerAvailability",
                "ecr:GetDownloadUrlForLayer",
                "ecr:GetRepositoryPolicy",
                "ecr:DescribeRepositories",
                "ecr:ListImages",
                "ecr:DescribeImages",
                "ecr:BatchGetImage",
                "ecr:InitiateLayerUpload",
                "ecr:UploadLayerPart",
                "ecr:CompleteLayerUpload",
                "ecr:PutImage"
            ],
            "Resource": [
                "*"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "codebuild:BatchGetBuilds",
                "codebuild:InvalidateProjectCache",
                "codebuild:StartBuild",
                "codebuild:StopBuild",
                "codebuild:UpdateProject",
                "codebuild:UpdateWebhook"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "sts:AssumeRole"
            ],
            "Resource": "arn:aws:iam::<AccountB>:role/taskexecutionrole",
            "Effect": "Allow"
        }
    ]
}

arn:aws:iam::<AccountB>:role/taskexecutionrole角色存在于AccountB中并信任AccountA。这是AccountB中的角色:

{
            "Effect": "Allow",
            "Action": "ecs:*",
            "Resource": [
                "*"
            ]
}

管道具有ECR源,在构建阶段会生成imagedefinitions.json文件。最后,部署阶段将进行ECS部署。

我得到的错误是: 无效的操作配置 标识符用于AccountB。您的accountId为AccountA

This答案仅对手动CLI部署有所帮助,我已经尝试过this答案中的解决方案。

任何指针我都缺少什么?

1 个答案:

答案 0 :(得分:2)

让我们假设:

Account_A => CodePipeline和源
Account_B => ECS

这是必需的:

帐户_A:
* AWSCodePipelineServiceRole
* Artifact_Store_S3_Bucket
* KMS_Key_for_Pipeline_Artifact(客户托管密钥)
* Artifact_Store_S3_Bucket上的存储桶策略以允许Account_B访问
*关于KMS_Key_for_Pipeline_Artifact的密钥策略,以允许访问Cross_Account_Role(来自Account_B)

帐户B
* Cross_Account_Role(具有Account_A和Full_ECS权限的信任关系)
*正在运行的ECS将被部署替换

imagedefinitions.json (必须是源代码的一部分)

[ 
    { 
      "name": "container_name", 
      "imageUri": "nginx:latest" 
    } 
]

Artifact_Store_S3_Bucket上的桶策略

{
    "Version": "2012-10-17",
    "Id": "SSEAndSSLPolicy",
    "Statement": [
        {
            "Sid": "DenyUnEncryptedObjectUploads",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": "arn:aws:s3:::Artifact_Store_S3_Bucket/*",
            "Condition": {
                "StringNotEquals": {
                    "s3:x-amz-server-side-encryption": "aws:kms"
                }
            }
        },
        {
            "Sid": "DenyInsecureConnections",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::Artifact_Store_S3_Bucket/*",
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                }
            }
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::Account_B:root"
            },
            "Action": [
                "s3:Get*",
                "s3:Put*"
            ],
            "Resource": "arn:aws:s3:::Artifact_Store_S3_Bucket/*"
        },
        {
            "Sid": "",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::Account_B:root"
            },
            "Action": "s3:ListBucket",
            "Resource": "arn:aws:s3:::Artifact_Store_S3_Bucket"
        }
    ]
}

pipeline.json:

{
    "pipeline": {
        "name": "test",
        "roleArn": "arn:aws:iam::Account_A:role/service-role/AWSCodePipelineServiceRole",
        "artifactStore": {
            "type": "S3",
            "location": "Artifact_Store_S3_Bucket",
            "encryptionKey": {
              "id": "arn:aws:kms:us-east-1:Account_A:key/KMS_Key_for_Pipeline_Artifact",
              "type": "KMS"
            }
        },
        "stages": [
            {
                "name": "Source",
                "actions": [
                    {
                        "name": "Source",
                        "actionTypeId": {
                            "category": "Source",
                            "owner": "AWS",
                            "provider": "CodeCommit",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "configuration": {
                            "BranchName": "master",
                            "PollForSourceChanges": "false",
                            "RepositoryName": "code"
                        },
                        "outputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "inputArtifacts": [],
                        "region": "us-east-1"
                    }
                ]
            },
            {
                "name": "Deploy",
                "actions": [
                    {
                        "name": "Deploy",
                        "actionTypeId": {
                            "category": "Deploy",
                            "owner": "AWS",
                            "provider": "ECS",
                            "version": "1"
                        },
                        "runOrder": 1,
                        "roleArn": "arn:aws:iam::Account_B:role/CrossAccount_Role",
                        "configuration": {
                            "ClusterName": "<Cluster>",
                            "ServiceName": "<Service>"
                        },
                        "outputArtifacts": [],
                        "inputArtifacts": [
                            {
                                "name": "SourceArtifact"
                            }
                        ],
                        "region": "us-east-1"
                    }
                ]
            }
        ],
        "version": 1
    }
}

要更新管道:

$ aws codepipeline update-pipeline --region us-east-1 --cli-input-json文件://pipeline.json

相关问题