Sub topic 12
Encrypting volumes and snapshots
This chapter assumes you can create and attach volumes, you know what a snapshot is, and you have used the AWS CLI. You don't need to know KMS in advance. The chapter introduces the parts of it that EBS uses.
What EBS encryption covers
EBS encryption protects data with AES-256 using keys managed by AWS Key Management Service (KMS). When a volume is encrypted, all of the following are encrypted too:
- Data at rest on the volume.
- Data moving between the instance and the volume.
- Every snapshot created from the volume.
- Every volume created from one of those snapshots.
You don't change anything inside the instance. The operating system sees an ordinary block device, and you format and mount it as usual. The encryption happens on the host that runs the instance, so applications need no changes. AWS documents the performance effect as minimal for supported instance types, but measure your own workload before relying on that.
How the keys work
EBS uses envelope encryption. A KMS key never leaves KMS and never encrypts your data directly. Instead:
- When you create an encrypted volume, EBS asks KMS for a unique data key for that volume.
- KMS returns the data key in two forms. One is plaintext and the other is encrypted under your KMS key.
- EBS stores the encrypted copy with the volume's metadata.
- When the volume is attached, EBS asks KMS to decrypt the data key. The plaintext key is held in memory on the host and is used to encrypt and decrypt I/O.
Because every attach requires KMS to decrypt the data key, the KMS key is a dependency of the volume. This has several consequences:
- If the key is disabled, or deleted after its waiting period, EBS can't decrypt the data key. The volume can't be attached and its data is unrecoverable.
- The identity that creates or attaches the volume needs KMS permissions on the key. These are
kms:CreateGrant,kms:Decrypt,kms:GenerateDataKeyWithoutPlaintext,kms:ReEncrypt*andkms:DescribeKey. - Calls to KMS are billed and appear in CloudTrail, which gives you an audit trail of key use.
Choosing a KMS key
| Key type | Who manages it | Can you share encrypted snapshots with other accounts? | Typical use |
|---|---|---|---|
| AWS managed key (`aws/ebs`) | AWS creates it in each Region. You can't edit its policy or rotate it yourself. | No | Simple, single-account setups |
| Customer managed key | You, through the key policy and IAM | Yes, if you grant the other account access to the key | Cross-account sharing, custom rotation, fine-grained access control |
If you might ever share a snapshot or an AMI with another account, use a customer managed key from the start. Snapshots encrypted with the AWS managed key can't be shared. Fixing this later means copying the snapshot and re-encrypting it.
Creating encrypted volumes
Pass --encrypted to create-volume. Without --kms-key-id, EBS uses the Region's default key, which is aws/ebs unless you changed it.
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type gp3 \
--size 100 \
--encrypted \
--kms-key-id alias/ebs-data-key \
--tag-specifications 'ResourceType=volume,Tags=[{Key=Name,Value=secure-data-vol}]'To check the result, list the encryption flag and key for your volumes:
aws ec2 describe-volumes \
--query 'Volumes[].{Id:VolumeId,Encrypted:Encrypted,Key:KmsKeyId}' \
--output tableTurning on encryption by default
Remembering --encrypted on every call doesn't scale. Encryption by default is an account setting that applies per Region. Once it is on, every new volume and every new snapshot copy in that Region is encrypted.
aws ec2 enable-ebs-encryption-by-default --region us-east-1
aws ec2 get-ebs-encryption-by-default --region us-east-1To make the default use your own key instead of aws/ebs, set it explicitly:
aws ec2 modify-ebs-default-kms-key-id \
--kms-key-id alias/ebs-data-key \
--region us-east-1Rules for snapshots and volumes
The encryption state of a snapshot determines what you can create from it:
| Source | What happens |
|---|---|
| Snapshot of an encrypted volume | The snapshot is encrypted, with the same KMS key. |
| Volume created from an encrypted snapshot | The volume is always encrypted. |
| Volume created from an unencrypted snapshot | The volume is unencrypted unless you request encryption at creation. |
| Copy of an unencrypted snapshot | You can make the copy encrypted. |
| Copy of an encrypted snapshot | The copy stays encrypted. You can re-encrypt it under a different key. |
There is no supported way to turn an encrypted volume or snapshot into an unencrypted one. If you need unencrypted data, copy the files at the file system level to a new unencrypted volume.
Encrypting an existing unencrypted volume
You can't flip a switch on an existing volume. The path is to snapshot it, then create an encrypted copy:
aws ec2 create-snapshot \
--volume-id vol-0123456789abcdef0 \
--description 'pre-encryption snapshot'
aws ec2 copy-snapshot \
--source-region us-east-1 \
--source-snapshot-id snap-0123456789abcdef0 \
--encrypted \
--kms-key-id alias/ebs-data-key \
--description 'encrypted copy'Wait until the copy reaches the completed state. Then create a volume from the encrypted copy in the Availability Zone of your instance, stop the instance, and swap the old volume for the new one. For a root volume, the replacement device name must match the original, for example /dev/xvda.
For a one-step alternative, create-volume --snapshot-id <unencrypted snapshot> --encrypted encrypts the new volume as it is created from the unencrypted snapshot.
Copying across Regions and accounts
Encrypted snapshots are copied with copy-snapshot, and the --region option names the destination. KMS keys are Regional, so the copy must be encrypted under a key that exists in the destination Region:
aws ec2 copy-snapshot \
--source-region us-east-1 \
--source-snapshot-id snap-0123456789abcdef0 \
--region us-west-2 \
--encrypted \
--kms-key-id alias/ebs-data-key-west \
--description 'DR copy in us-west-2'For cross-account sharing, the sequence is:
- Encrypt the snapshot with a customer managed key.
- Add the other account to the key policy, or grant access to it.
- Share the snapshot with the other account.
- In the other account, copy the snapshot and re-encrypt it under a key that account owns. This stops the copy from depending on your key.
Common errors
- Volume stuck in an unusable state after a key change. The KMS key was disabled or scheduled for deletion. Re-enable the key, or cancel the deletion before the waiting period ends. After the key is deleted, nothing can recover the data.
Client.InvalidKMSKey.InvalidStateor access denied on launch. The caller lacks the KMS permissions listed above, or the key policy doesn't allow the account. Check both the IAM policy and the key policy.- Can't share a snapshot. It uses the AWS managed key. Copy it under a customer managed key first.
- Instance launch fails after enabling default encryption. An AMI or template specifies an unencrypted block device mapping. Update it to use encryption.
Exercise
Your team's account has encryption by default enabled in us-east-1 with the aws/ebs key. A partner account needs a copy of a snapshot of one volume. The sharing attempt fails. What is the cause and the fix?
Show answer
Snapshots encrypted with the AWS managed key (aws/ebs) can't be shared because the partner account can't be granted access to that key. Copy the snapshot with copy-snapshot --encrypted --kms-key-id pointing at a customer managed key, allow the partner account in that key's policy, and share the new copy. The partner then copies it into a key of their own. To avoid repeating this, change the Region default with modify-ebs-default-kms-key-id to a customer managed key.
Exercise
You enabled encryption by default today. Does an unencrypted volume created last week become encrypted? How would you encrypt it?
Show answer
No. The setting applies only to new volumes and new snapshot copies. Snapshot the old volume, copy the snapshot with --encrypted and a KMS key (or create a volume from the snapshot with --encrypted), then replace the old volume with the new encrypted one.