Cho phép ECS ở AWS account A pull ECR Images tại AWS account B

Bước 1:

Tạo permission policy cho ECR repository chứa image mà khách hàng muốn chỉ định:

  • Mở ECR console

  • Chọn tên Repository chứa image mong muốn

  • Ở naviagation pane bên tay trái, chọn “Permissions”. Chọn “Edit policy JSON” và dán vào đó policy:

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPushPull",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-id:root"
            },
            "Action": [
                "ecr:GetDownloadUrlForLayer",
                "ecr:BatchGetImage",
                "ecr:BatchCheckLayerAvailability",
                "ecr:PutImage",
                "ecr:InitiateLayerUpload",
                "ecr:UploadLayerPart",
                "ecr:CompleteLayerUpload"
            ]
        }
    ]
}

Thay account-id ở “arn:aws:iam::account-id:root” thành id của account môi trường thật. (Ảnh 111796_ecr_repo_permission.png)

Bước 2:

Trong task definition tạo ở môi trường thật, chỉ định image uri như sau:

aws_account_id.dkr.ecr.region.amazonaws.com/my-repository:<tag>

Có thể lấy uri này bằng cách chọn Copy URI của image trên console ECR ở môi trường phát triển

Lưu ý: Hãy chắc chắn rằng Task định nghĩa ở môi trường thật có các quyền ECR được liệt kê trong managed policy (với fargate, còn với ec2 launch type thì như trong link tài liệu) “AmazonECSTaskExecutionRolePolicy”

Tài liệu:

https://aws.amazon.com/premiumsupport/knowledge-center/ecs-tasks-pull-images-ecr-repository/?nc1=h_ls

https://aws.amazon.com/premiumsupport/knowledge-center/secondary-account-access-ecr/?nc1=h_ls
⇒ Comment: Cách làm của ec2 launch type và fargate như nhau, chỉ khác ở đoạn role trong task definition thôi