Sub topic 12
Replicating images across Regions and accounts
ECR replication copies images from one private registry to another Region, another account, or both. This chapter covers how replication rules work, how to set up cross-Region and cross-account replication, and what is and isn't copied. It assumes you can push images to a repository, you know the difference between tags and digests, and you have read the earlier chapters on IAM and repository policies.
Why replicate
Replication is useful in three common cases:
- Latency and transfer cost. A cluster in
eu-west-1pulls from a registry in the same Region instead of reaching across the Atlantic. - Disaster recovery. If the source Region is unavailable, the images your workloads need already exist elsewhere.
- Account separation. A build account pushes images once, and production accounts receive copies. Production never needs permission to push or to read the build account's repositories.
Replication is not a backup of your repository settings, and it is not a two-way sync. It moves image content from a source to destinations, in one direction.
How replication works
Replication is configured on the private registry, not on a single repository. A registry holds one replication configuration made of rules. Each rule has two parts:
- Destinations. Each destination is a Region plus a registry ID, which is an AWS account ID. A destination can be another Region in your own account, the same Region in another account, or a different Region in another account.
- Repository filters (optional). Each filter is a prefix. Only repositories whose names start with the prefix are replicated by that rule. With no filter, every repository in the registry is replicated.
When you push an image to a matching repository, ECR copies it to each destination. The copy keeps the same repository name, the same tags, and the same image digest, because the manifest and layers are copied unchanged. A digest reference such as my-app@sha256:... therefore points to identical content in every Region. That matters for reproducible deployments.
What triggers a copy
Replication runs when an image is pushed after the rule exists. Three consequences follow:
- Images that were already in the repository when you created the rule are not copied. To seed a destination, push the image again or copy it yourself with Docker (pull, tag, push to the destination).
- Replication is asynchronous. A push returns when the source has the image, and the copy usually appears within minutes. Do not make a deployment depend on the destination having the image the moment the push finishes.
- A push that only adds a tag to an existing image counts as a push, so the new tag is replicated.
What is copied and what is not
Replication copies images (manifests, layers, tags). It does not copy repository configuration. Each destination repository has its own:
- repository policy
- lifecycle policy
- tag immutability setting
- scanning configuration
- encryption setting
If the destination repository does not exist, ECR creates it when the first image arrives. The new repository gets default settings unless a repository creation template in the destination registry matches its name. Templates let you apply immutability, scanning, encryption, lifecycle rules, and policies to repositories that replication creates. Without a template, a replicated repository is mutable, uses AES-256 encryption, and has no lifecycle policy. Storage in a replica then grows without limit.
Setting up cross-Region replication
Within one account, you only need to create the replication configuration. Save this as replication.json. It replicates every repository whose name starts with prod- from the source registry to us-west-2 in the same account:
{
"rules": [
{
"destinations": [
{
"region": "us-west-2",
"registryId": "123456789012"
}
],
"repositoryFilters": [
{
"filter": "prod-",
"filterType": "PREFIX_MATCH"
}
]
}
]
}Apply it in the source Region:
aws ecr put-replication-configuration \
--replication-configuration file://replication.json \
--region us-east-1The call replaces the whole configuration. If you want to add a rule, retrieve the current configuration with aws ecr describe-registry, edit it, and submit the combined result. Submitting only the new rule removes the existing ones.
The first time you configure replication, ECR creates a service-linked role named AWSServiceRoleForECRReplication in your account. ECR uses it to perform the copies. The identity that calls put-replication-configuration needs ecr:PutReplicationConfiguration, plus iam:CreateServiceLinkedRole the first time so the role can be created. The source identity also needs ecr:CreateRepository and ecr:ReplicateImage for the destination, which for the same account are normally covered by the service-linked role.
Setting up cross-account replication
Cross-account replication needs two grants, one on each side:
- In the source account, the replication configuration names the destination account in
registryId. - In the destination account, a registry permissions policy must allow the source account to create repositories and replicate images into it.
A registry permissions policy is attached to the whole registry, not a repository. It is a different object from the repository policy you used for cross-account pulls. Run this in the destination account (999988887777) and in the destination Region:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReplicationFromBuildAccount",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::123456789012:root"
},
"Action": [
"ecr:CreateRepository",
"ecr:ReplicateImage"
],
"Resource": "arn:aws:ecr:us-east-1:999988887777:repository/*"
}
]
}aws ecr put-registry-policy \
--policy-text file://registry-policy.json \
--region us-east-1Then, in the source account, point a rule at the destination account:
{
"rules": [
{
"destinations": [
{
"region": "us-east-1",
"registryId": "999988887777"
}
]
}
]
}You can narrow the Resource in the registry policy to a prefix such as repository/prod-* so the source account can only create and write repositories you expect.
Verifying replication
Check the registry configuration:
aws ecr describe-registry --region us-east-1Then list images in the destination:
aws ecr describe-images \
--repository-name prod-my-app \
--region us-west-2 \
--registry-id 123456789012 \
--query 'imageDetails[].{tags:imageTags,digest:imageDigest,pushed:imagePushedAt}'Compare the digests with the source. They must match. If an image is missing after several minutes, check these in order:
- The repository name starts with the filter prefix. The match is case-sensitive.
- The image was pushed after the rule was created.
- For cross-account rules, the destination registry policy allows
ecr:CreateRepositoryandecr:ReplicateImagefrom the source account, and itsResourcematches the destination Region. - The destination repository, if it already exists, does not have tag immutability on with an existing tag of the same name pointing at a different image. In that case the tag cannot be updated.
- The CloudTrail events from
ecr.amazonaws.comin the source account show the replication attempts and any access denied errors.
Using replicas at runtime
A workload pulls from the registry in its own Region, so its image URI changes per Region:
123456789012.dkr.ecr.us-east-1.amazonaws.com/prod-my-app@sha256:...123456789012.dkr.ecr.us-west-2.amazonaws.com/prod-my-app@sha256:...
Because the digest is identical, deployment templates can keep one digest value and substitute only the registry host. Deploying by tag works too, but only if tags are immutable in the destination. Otherwise a replica could differ from its source after a later overwrite.
Costs and limits
Replication has a cost. You pay for storage in every destination, and for data transferred between Regions. Transfer between accounts in the same Region does not leave the Region, but storage is still duplicated. Rules and destinations per registry are limited, so check the current ECR service quotas before designing a large fan-out. Replication also does not cascade. A copy that arrives in a destination is not re-replicated by that destination's own rules, so a chain such as A to B to C needs a rule from A for each target.
Summary of decisions
| Need | Approach |
|---|---|
| Faster pulls in another Region of the same account | Cross-Region rule, registry ID is your own account |
| Promote images from a build account to a production account | Cross-account rule plus a registry policy in the destination |
| Replicate only some repositories | Repository filter with a name prefix |
| Replicas should be immutable and expire old images | Repository creation templates in the destination registry |
| Seed a destination with existing images | Re-push, or pull, tag, and push to the destination |
Exercise
Your team creates a replication rule on Monday that copies all repositories from us-east-1 to eu-west-1. On Tuesday you find that the repository my-app in eu-west-1 contains only the images pushed since Monday, and its tags are mutable even though the source repository has immutable tags. Explain both observations and describe what you would change.
Show answer
Replication copies only images pushed after a rule exists, so older images in the source were never copied. They can be seeded by pushing them again or by pulling, tagging, and pushing them to the destination. Replication also does not copy repository settings such as tag immutability. ECR created the destination repository with default settings (mutable tags). To fix it, set tag immutability on the destination repository, and define a repository creation template in the eu-west-1 registry that applies immutability, scanning, and lifecycle rules to repositories that replication creates in the future.