Sub topic 11
Encryption at Rest with AES-256 or KMS
This chapter assumes you can create an ECR repository with the AWS CLI. It also assumes you know what an IAM role and a KMS key are at a basic level. The chapter covers how ECR encrypts stored images and how to choose between the two encryption types.
What ECR encrypts
ECR encrypts every image stored in a repository. You can't turn encryption off. You choose only which key does the work. The encryption setting belongs to the repository, and you pick it when you create the repository.
ECR supports two encryption types:
| Type | CLI value | Key owner | Typical reason to use it |
|---|---|---|---|
| Server-side encryption with Amazon S3-managed keys (AES-256) | `AES256` | AWS | Default. No setup and no extra cost. |
| AWS Key Management Service (KMS) | `KMS` | You, or AWS on your behalf | You need control over the key, key policies, or audit trails in CloudTrail. |
If you don't specify a type, ECR uses AES256.
How the KMS option works
With KMS, ECR uses a key in AWS KMS to protect image layers. You can use the AWS managed key with the alias aws/ecr, or a customer managed key that you create. ECR creates the aws/ecr key in your account the first time you create a KMS-encrypted repository, if it doesn't already exist.
A customer managed key gives you more control:
- You write the key policy and decide who can use the key.
- You can disable or schedule deletion of the key.
- Each use of the key is logged in AWS CloudTrail.
- You can enable automatic key rotation.
ECR doesn't call KMS directly with your credentials. It uses a grant, which is a temporary permission that KMS creates for ECR when you push or pull. The grant lets ECR encrypt and decrypt layers for that repository. The IAM identity that creates the repository needs permission to create grants on the key, such as kms:CreateGrant, kms:DescribeKey, and kms:RetireGrant.
Creating a repository with KMS
This command creates a repository encrypted with a customer managed key. Replace the key ARN with one from your account.
aws ecr create-repository \
--repository-name my-secure-app \
--encryption-configuration encryptionType=KMS,kmsKey=arn:aws:kms:us-east-1:123456789012:key/11111111-2222-3333-4444-555555555555 \
--region us-east-1To use the AWS managed key instead, leave out kmsKey:
aws ecr create-repository \
--repository-name my-secure-app \
--encryption-configuration encryptionType=KMS \
--region us-east-1To check what a repository uses, describe it:
aws ecr describe-repositories \
--repository-names my-secure-app \
--region us-east-1 \
--query 'repositories[].encryptionConfiguration'Common errors and gotchas
- You can't change the setting later. The encryption configuration is fixed at creation. To switch types, create a new repository and copy the images into it.
- Key problems break access. If you disable the key or delete it, ECR can't decrypt the layers. Pushes and pulls fail until the key is usable again.
- Missing KMS permissions. If creation fails with an access denied error, check that your IAM identity can use the key and create grants on it.
- Cross-account pulls. A repository policy alone doesn't cover everything when the repository uses a customer managed key. The key policy must also allow the pulling account.
- Encryption at rest is not access control. Anyone with ECR permissions can still pull images in readable form. IAM and repository policies decide who can read.
Choosing between the two
Use AES256 when you only need the baseline protection. It is simpler and has no KMS cost. Use KMS when a compliance rule requires you to control the key, when you want to revoke access by disabling a key, or when you need key usage in your audit logs. KMS requests are billed, and a customer managed key has a monthly charge.
Exercise
A team's security policy says they must be able to disable the encryption key to cut off all access to stored images, and every key use must appear in CloudTrail. Which ECR encryption type should they pick, and what must they decide before creating the repository?
Show answer
They should use KMS with a customer managed key. They must decide this before creating the repository, because the encryption configuration can't be changed afterward. To switch later, they would have to create a new repository and copy the images over.