Sub topic 12
Encrypting volumes and snapshots
This chapter assumes you can create, attach and mount an EBS volume, and that you know what a snapshot is. You should also know that AWS KMS (Key Management Service) holds encryption keys. EBS encryption protects data at rest, data moving between the instance and the volume, and snapshots made from the volume.
How EBS encryption works
When you create an encrypted volume, EBS asks KMS for a data key under a KMS key you choose. The data key is stored, encrypted, with the volume. The Nitro hardware on the host decrypts it and encrypts and decrypts I/O as it passes between the instance and the volume. Your operating system and applications see ordinary plaintext, and you pay no extra EBS charge for encryption.
Encryption carries through the chain of copies:
- Snapshots of an encrypted volume are encrypted.
- Volumes created from an encrypted snapshot are encrypted.
- Data in transit between the instance and the volume is encrypted.
Choosing a KMS key
You can use the AWS managed key aws/ebs or a customer managed key. The managed key needs no setup. A customer managed key lets you set the key policy, rotate on your own schedule, grant access to other accounts, and disable the key. Use one when you need to control who can use the key or share encrypted snapshots.
To create an encrypted volume with a customer managed key, add --kms-key-id:
aws ec2 create-volume \
--availability-zone us-east-1a \
--volume-type gp3 \
--size 100 \
--encrypted \
--kms-key-id alias/my-ebs-keyIf you pass --encrypted without a key, EBS uses the default key for the Region, which is aws/ebs unless you changed it.
Turning on encryption by default
Encryption by default is a per-Region account setting. After you enable it, every new volume and every snapshot copy in that Region is encrypted. Existing volumes are not changed.
aws ec2 enable-ebs-encryption-by-default --region us-east-1
aws ec2 get-ebs-encryption-by-default --region us-east-1Because the setting is per Region, repeat it in each Region you use. Once enabled, you can't create unencrypted volumes in that Region. Some older instance types don't support encryption, so check support before you enable it.
Encrypting an existing unencrypted volume
You can't switch encryption on for a volume in place. Instead, copy it through a snapshot:
- Create a snapshot of the unencrypted volume.
- Copy the snapshot with encryption enabled and a KMS key.
- Create a new volume from the encrypted copy.
- Detach the old volume, attach the new one, and delete the old volume once you've verified the data.
aws ec2 copy-snapshot \
--source-region us-east-1 \
--source-snapshot-id snap-0123456789abcdef0 \
--encrypted \
--kms-key-id alias/my-ebs-key \
--description 'encrypted copy'Common errors
- Client.InvalidKMSKey.InvalidState or an instance that won't start. The KMS key is disabled or scheduled for deletion. The instance can't decrypt its volumes until the key is usable again.
- Access denied when launching or attaching. The calling identity needs KMS permissions such as
kms:CreateGrant,kms:Decryptandkms:GenerateDataKeyWithoutPlaintexton the key. - Can't share a snapshot. Snapshots encrypted with
aws/ebscan't be shared with other accounts. Use a customer managed key and add the other account to its key policy. - Expecting a change to existing data. Encryption by default doesn't convert volumes that already exist.
Exercise
Your account has an unencrypted volume in us-east-1, and you've enabled encryption by default in that Region. Is the volume now encrypted? What steps would you take to get an encrypted copy of its data?
Show answer
No. Encryption by default affects only new volumes and snapshot copies. Create a snapshot of the volume, copy it with --encrypted (and optionally a KMS key), then create a new volume from the encrypted copy and swap it in for the old one.
Exercise
You want to share an encrypted snapshot with another AWS account. Which key type must protect the snapshot, and what else must you configure?
Show answer
A customer managed KMS key, because snapshots encrypted with the aws/ebs managed key can't be shared. Grant the other account access in the key policy, and share the snapshot with that account.