Sub topic 12
Replicating images across Regions and accounts
This chapter assumes you can push images to an ECR private repository, read IAM policy JSON, and use the AWS CLI. It builds on the earlier chapters on repository policies and tags.
Why replicate
ECR stores each repository in one Region. Replication copies images to other Regions or accounts automatically, so you don't have to push each image several times. Common reasons:
- Clusters in another Region pull from a nearby registry, which lowers pull latency and avoids cross-Region data transfer.
- A second Region holds a copy of your images if the first Region is unavailable.
- A production account receives images that were built in a separate build account.
How replication works
Replication is a setting on the private registry, not on a single repository. You define rules. Each rule lists destinations (a Region and an account ID) and, optionally, repository filters.
When an image is pushed to a matching repository, ECR copies it to each destination asynchronously. If the destination repository doesn't exist, ECR creates it.
Keep these limits in mind:
- Only images pushed after you create the rule are replicated. Existing images are not copied. Push them again to replicate them.
- Tags and digests are preserved, so a digest reference works the same in every destination.
- Replication copies images only. It does not copy repository policies, lifecycle policies, or tag immutability and scanning settings. Configure those on the destination repositories yourself.
- A registry supports up to 10 rules and 25 unique destinations.
Replicating to another Region
Save this rule as replication.json. It copies repositories whose names start with my-app to us-west-2 in the same account and to eu-west-1 in account 111122223333.
{
"rules": [
{
"destinations": [
{ "region": "us-west-2", "registryId": "123456789012" },
{ "region": "eu-west-1", "registryId": "111122223333" }
],
"repositoryFilters": [
{ "filter": "my-app", "filterType": "PREFIX_MATCH" }
]
}
]
}Apply it in the source Region:
aws ecr put-replication-configuration \
--replication-configuration file://replication.json \
--region us-east-1Your IAM identity needs ecr:PutReplicationConfiguration, ecr:CreateRepository, and ecr:ReplicateImage. ECR creates the service-linked role AWSServiceRoleForECRReplication the first time you set this up.
Allowing replication into another account
A destination in a different account must grant permission. You do this with a registry permissions policy in the destination account. It applies to the whole registry in that Region, which is different from a repository policy.
Run this in account 111122223333:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReplicationFromSource",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:root"
},
"Action": [
"ecr:CreateRepository",
"ecr:ReplicateImage"
],
"Resource": "arn:aws:ecr:eu-west-1:111122223333:repository/*"
}
]
}aws ecr put-registry-policy \
--policy-text file://registry-policy.json \
--region eu-west-1Checking results and common errors
Replication is not instant. Wait a few minutes after a push, then list images in the destination:
aws ecr describe-images \
--repository-name my-app \
--region us-west-2 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest}'If an image is missing, check these causes:
| Symptom | Likely cause |
|---|---|
| Image pushed before the rule existed | Replication only covers new pushes. Push again. |
| Nothing arrives in another account | The destination has no registry policy, or it is in a different Region than the destination. |
| Repository name doesn't match | The filter is a prefix match on the repository name. Check the spelling. |
| Destination repo allows tag overwrite | Replicated repos are created with default settings. Set immutability and scanning yourself. |
Exercise
You added a replication rule to us-west-2 yesterday. Today you notice that the 20 images pushed last month are missing there, but new pushes appear. Why, and what do you do?
Show answer
Replication only copies images pushed after the rule exists. To copy the older images, push them again to the source repository (the same tag and digest content is then replicated), or copy them manually with docker pull and docker push to the destination.