Sub topic 11
Encryption at rest with AES-256 or KMS
This chapter covers how Amazon ECR encrypts stored image data and how to choose between the default encryption and AWS Key Management Service (KMS). It assumes you can create a repository with the AWS CLI, you know the basics of IAM policies, and you have a general idea of what KMS keys are.
What gets encrypted
ECR stores image layers and manifests in the repository. Encryption at rest means that ECR encrypts this stored data on the server side before it is written to disk, and decrypts it when you pull an image. You do not encrypt anything in the Docker client, and docker push and docker pull work the same way for every option.
Encryption at rest protects the stored bytes. It does not control who can pull an image. Access is still decided by IAM policies and repository policies. Data in transit is protected separately, because Docker talks to the registry over HTTPS.
The two encryption types
Each repository uses one of two settings.
| Setting | Key owner | Key management | Extra cost |
|---|---|---|---|
| `AES256` (default) | Amazon S3 | Fully managed by AWS, no key to configure | None |
| `KMS` with the AWS managed key `aws/ecr` | AWS KMS, in your account | AWS creates and rotates it, and you can see it in KMS | KMS charges apply |
| `KMS` with a customer managed key | You | You control the key policy, rotation, disabling, and deletion | KMS key and request charges apply |
AES256 is what you get if you do not specify anything. It encrypts every repository with no setup. Choose KMS when you need one of the following:
- An audit trail of key use in AWS CloudTrail.
- Control over who can use the key, separate from who can use ECR.
- The ability to cut off access to the data by disabling the key.
- A compliance requirement that names KMS or customer managed keys.
The setting is fixed at creation
You choose the encryption configuration when you create the repository. You cannot change it afterward. To move to a different key type, create a new repository with the configuration you want, push or copy the images into it, and delete the old one. Decide before you create the repository.
This also means a team policy such as "all repositories use our customer managed key" must be applied at creation time. Repository creation templates (used with pull through cache and replication) can carry an encryption setting for repositories that ECR creates for you.
Creating a KMS-encrypted repository
First create a customer managed key. Then pass its ARN in --encryption-configuration.
KEY_ARN=$(aws kms create-key \
--description "ECR encryption for my-app-kms" \
--query KeyMetadata.Arn \
--output text \
--region us-east-1)
aws ecr create-repository \
--repository-name my-app-kms \
--encryption-configuration encryptionType=KMS,kmsKey="$KEY_ARN" \
--image-tag-mutability IMMUTABLE \
--image-scanning-configuration scanOnPush=true \
--region us-east-1If you set encryptionType=KMS and leave out kmsKey, ECR uses the AWS managed key aws/ecr. The key must be in the same Region as the repository.
To check what a repository uses:
aws ecr describe-repositories \
--region us-east-1 \
--query 'repositories[].{name:repositoryName,encryption:encryptionConfiguration}'A default repository shows encryptionType as AES256 with no key. A KMS repository shows KMS and a kmsKey ARN.
How ECR uses your key
ECR does not use the permissions of the person who runs docker push. When you create a KMS repository, ECR creates a KMS grant. A grant is a delegated permission that lets ECR use the key for that repository. ECR retires the grant when the repository is deleted.
Layer data is encrypted with the key when it is stored. ECR includes the repository ARN as an encryption context, so a key use shows up in CloudTrail tied to the repository it served.
The practical consequence is that your pushers and pullers need no KMS permissions of their own. They need only the usual ECR permissions covered in earlier chapters. The administrator who creates the repository does need KMS permissions, as described next.
Permissions for the repository creator
The principal that calls create-repository with a customer managed key must be allowed to create the grant. Deleting the repository requires retiring it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEcrRepoCreationWithCmk",
"Effect": "Allow",
"Action": [
"kms:CreateGrant",
"kms:DescribeKey",
"kms:RetireGrant"
],
"Resource": "arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab"
}
]
}Replace the key ARN with your own. The key policy must also allow the creator, either directly or by delegating to IAM through the standard "Enable IAM policies" statement. A KMS key is usable only if its key policy permits it, so an IAM policy alone is not enough when the key policy does not delegate to IAM.
Scoping Resource to the specific key follows least privilege. The same policy with "Resource": "*" would let the principal create grants on every key in the account.
What happens when the key is unavailable
With KMS encryption, your key becomes a dependency of every push and pull. If the key is disabled, scheduled for deletion, or its grant is revoked, ECR cannot decrypt the layers. Pulls fail, and so do the deployments that depend on them, such as new ECS tasks and EKS pods.
This is a useful property when you want to be able to shut off access to the data. It is also a risk when someone disables or deletes the key by mistake. Consider these safeguards:
- Restrict
kms:DisableKey,kms:ScheduleKeyDeletion, andkms:RevokeGrantto a small group of administrators. - Alarm on CloudTrail events for those actions.
- Remember that KMS key deletion has a waiting period of 7 to 30 days, which gives you time to cancel it.
- Never delete a key that still protects a repository you need.
Cost and quota considerations
AES-256 adds no cost. Customer managed keys have a monthly charge, and every KMS request is billed. ECR calls KMS when it reads and writes layers, so a repository with heavy pull traffic generates KMS requests. For most teams this cost is small, but check it in a busy environment. The AWS managed key aws/ecr has no monthly key fee, though request charges still apply.
KMS also has request rate quotas per Region. A very large fleet pulling many images at once, for example during a mass redeploy, adds load to those quotas. If you see throttling errors, request a quota increase for KMS.
Replication and encryption
Encryption is a property of each repository. When you replicate images to another Region or account, the destination repository has its own encryption configuration. Replication does not carry your source key to the destination, and a key in one Region cannot be used in another. Plan a key in each destination Region if you need KMS there. Repository creation templates are the way to make auto-created destination repositories use a KMS key instead of the default.
Choosing between the options
Use this checklist:
- Does a requirement, auditor, or internal standard name KMS or customer managed keys? If yes, use a customer managed key.
- Do you need to revoke access to image data by disabling a key? If yes, use a customer managed key.
- Do you want KMS logging in CloudTrail but no key management? Use the AWS managed key
aws/ecr. - Otherwise, keep the default
AES256. It encrypts your data at rest with no setup, cost, or extra failure mode.
Because you cannot change the setting later, lean toward the stricter option when a repository will hold sensitive images long term.
Common mistakes
- Expecting to change encryption on an existing repository. You must create a new repository and move the images.
- Using a key from the wrong Region. The key and repository must be in the same Region.
- Forgetting the creator's KMS permissions.
create-repositoryfails if the caller cannot create the grant. - Giving every pusher KMS permissions. This is unnecessary. ECR uses its own grant.
- Treating encryption as access control. An image encrypted with KMS can still be pulled by anyone the ECR policies allow.
Exercise
Your security team requires that image data in a new repository be protected by a key your team can disable. A colleague proposes creating the repository with default settings and switching to a customer managed key afterward. Why does this fail, and what should you do instead?
Show answer
The encryption configuration of a repository is set at creation and cannot be changed later. Create a customer managed KMS key in the same Region, then create the repository with --encryption-configuration encryptionType=KMS,kmsKey=<key ARN>. The creator needs kms:CreateGrant and kms:DescribeKey on the key, and kms:RetireGrant for deletion. Image pushers and pullers need only ECR permissions.
Exercise
A deployment fails because ECS tasks cannot pull an image from a KMS-encrypted repository, although the task execution role has the correct ECR permissions and the image exists. CloudTrail shows the key was disabled an hour ago. What is the cause, and how do you fix it?
Show answer
ECR cannot decrypt the layers while the key is disabled, so pulls fail regardless of ECR permissions. Re-enable the key in KMS (or cancel its scheduled deletion if it was pending). Then restrict kms:DisableKey and kms:ScheduleKeyDeletion and add an alarm on those CloudTrail events.