Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS Fargate pull image from a cross account ECR repo

I have 2 AWS accounts: - account A that has an ECR repo. - account b that has an ECS cluster running Fargate.

I have created a "cross-account" role in account A with trust relations to account B, also I have attached the "AmazonEC2ContainerRegistryPowerUser" policy to this role.

I gave access to the ECR repository in account A by adding account B's id and the "cross-account" role to the repository policy.

I attached a policy to the fargate "TaskExecutionRole" allowing fargate to assume the "cross-account" role.

When trying to deploy a Fargate task in account B with a reference to an image in account A I'm getting a 500 error.

like image 518
Anthony Khodr Avatar asked Oct 21 '18 11:10

Anthony Khodr


People also ask

How do I copy a Docker image from one ECR to another?

In order to transfer a Docker image from one server to another, what you need to do is first export the image to a file, then copy that file over from your current server to the new one using scp or rsync and finally load the image to your new server.


1 Answers

Fargate will not automatically assume a cross-account role. Fortunately, you do not need to assume a role in another account in order to pull images from that account's ECR repository.

To enable cross-account access to an image in ECR, add access for account B in account A's repository (by setting the repository policy), and then specify a TaskExecutionRole in account B that has permissions to pull from ECR ("ecr:GetDownloadUrlForLayer", "ecr:BatchGetImage", "ecr:BatchCheckLayerAvailability").

For example, set a repository policy on the repository in account A like the following:

{
  "Version": "2008-10-17",
  "Statement": [
    {
      "Sid": "AllowCrossAccountPull",
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::ACCOUNT_B_ID:root"
      },
      "Action": [
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchCheckLayerAvailability",
        "ecr:BatchGetImage"
      ]
    }
  ]
}

Then, set your TaskExecutionRole in account B to have a policy like this:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage"
      ],
      "Resource": "*"
    }
  ]
}

Alternately, you can use the managed policy AmazonECSTaskExecutionRolePolicy for your TaskExecutionRole instead of defining your own.

like image 187
Samuel Karp Avatar answered Oct 05 '22 13:10

Samuel Karp