Encrypted EBS volumes for EC2 instances
Support encrypted EBS volumes for EC2 instances.
Use Case
We are using the CDK to generate EC2 fleets with EBS volumes that will contain customer sensitive information. We want customers to be able to use AMIs that we make available; the way to have a public AMI launch an encrypted instance is through this new EBS functionality.
Proposed Solution
I think that the option is via AWS::EC2::Instance → BlockDeviceMappings→ebs→ Encrypted/KmsKeyId, or AWS::EC2::LaunchTempate→BlockDeviceMappings..., or AWS::EC2::SpotFleet→ BlockDeviceMappings...
There does not appear to be a way to specify information that would feed into the BlockDeviceMapping in the L2 ec2.Instance construct
Other
Details: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIEncryption.html
- [ ] :wave: I may be able to implement this feature request
- [ ] :warning: This feature might incur a breaking change
This is a :rocket: Feature Request
Indeed, It'll be good to create EbsVolumes and be able to set a CMK (Alias/Key). Actually I have to create a "ec2.CfnLaunchTemplate" to do that.
Any news about adding encryption & use of CMK in BlockDeviceVolume of an aws-autoscaling for example ?
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-autoscaling.BlockDeviceVolume.html
Any news about adding encryption & use of CMK in BlockDeviceVolume of an aws-autoscaling for example ?
https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-autoscaling.BlockDeviceVolume.html
@Cloudrage The API in the docs you reference are eventually used to create a LaunchConfiguration for the AutoScalingGroup, unfortunately, LaunchConfiguration doesn't support this property:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-launchconfig-blockdev-template.html
It is however supported in LaunchTemplate: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-blockdevicemapping-ebs.html
but aws-autoscaling still doesn't support LaunchTemplates.
There is an outstanding PR to add LaunchTemplate support though, so once its merged I believe this functionality will be available as well.
That's the deal, I'm requesting/waiting for those 2 supports :
- LaunchTemplates
Encryptionprop for BlockDeviceVolume
Keep waiting...
The ability to add CMK to BlockDeviceVolume is much needed. I can specify a CMK while creating EC2 via AWS Console. Just need the same ability in CDK.
Is there any progress on this?
FWIW, you can use the AWS-Managed key by setting the corresponding property in EbsDeviceOptions:
blockDevices: [
{
deviceName: '/dev/xvda',
mappingEnabled: true,
volume: ec2.BlockDeviceVolume.ebs(30, {
deleteOnTermination: true,
volumeType: ec2.EbsDeviceVolumeType.GP2,
encrypted: true
})
}
]
FWIW, you can use the AWS-Managed key by setting the corresponding property in
EbsDeviceOptions:blockDevices: [ { deviceName: '/dev/xvda', mappingEnabled: true, volume: ec2.BlockDeviceVolume.ebs(30, { deleteOnTermination: true, volumeType: ec2.EbsDeviceVolumeType.GP2, encrypted: true }) } ]
Thanks for quick reply but how can I use my own KMS key (CMK) for encryption? I do not seem to have any option here?
FWIW, you can use the AWS-Managed key by setting the corresponding property in
EbsDeviceOptions:blockDevices: [ { deviceName: '/dev/xvda', mappingEnabled: true, volume: ec2.BlockDeviceVolume.ebs(30, { deleteOnTermination: true, volumeType: ec2.EbsDeviceVolumeType.GP2, encrypted: true }) } ]Thanks for quick reply but how can I use my own KMS key (CMK) for encryption? I do not seem to have any option here?
Building on top of what @shamelesscookie pointed out, as this creates a good foundation in the underlying CloudFormation template, in Java CDK (and probably other languages) you can utilize the concept of IAspect & CfnResource to override/extended provided implementation, and in this case encrypt the root device using CMK.
Conceptually what you basically have to do is the following:
- Make accessible the KmsKeyId you want to use from your Stack (if you are creating one as part of the Stack), or just pass the input if the key already exists.
- In the
visit(IConstruct node)method of the aspect implementation class you can do something like:
if (node != null && node instanceof Instance) {
CfnResource cfnResource = (CfnResource) node.getNode().findChild("Resource");
cfnResource.addPropertyOverride("BlockDeviceMappings.0.Ebs.KmsKeyId", this.kmsKeyId);
}
- In the App class of your CDK, add this after your instantiating both your stack and encryption aspect
Aspects.of(myStack).add(encryptionAspectClass);
This will encrypt your root device volume with the CMK, and every other volume you can provision using the Volume construct which does take custom encryption key. The concept of overriding properties in form of Escape Hatches is also explained here. You can check it out for more details. Hope it helps.
I think the PR is up for grabs since it wasn't updated for a long time by the original contributor.
FWIW, you can use the AWS-Managed key by setting the corresponding property in
EbsDeviceOptions:blockDevices: [ { deviceName: '/dev/xvda', mappingEnabled: true, volume: ec2.BlockDeviceVolume.ebs(30, { deleteOnTermination: true, volumeType: ec2.EbsDeviceVolumeType.GP2, encrypted: true }) } ]Thanks for quick reply but how can I use my own KMS key (CMK) for encryption? I do not seem to have any option here?
Building on top of what @shamelesscookie pointed out, as this creates a good foundation in the underlying CloudFormation template, in Java CDK (and probably other languages) you can utilize the concept of
IAspect&CfnResourceto override/extended provided implementation, and in this case encrypt the root device using CMK.Conceptually what you basically have to do is the following:
- Make accessible the KmsKeyId you want to use from your Stack (if you are creating one as part of the Stack), or just pass the input if the key already exists.
- In the
visit(IConstruct node)method of the aspect implementation class you can do something like:if (node != null && node instanceof Instance) { CfnResource cfnResource = (CfnResource) node.getNode().findChild("Resource"); cfnResource.addPropertyOverride("BlockDeviceMappings.0.Ebs.KmsKeyId", this.kmsKeyId); }
- In the App class of your CDK, add this after your instantiating both your stack and encryption aspect
Aspects.of(myStack).add(encryptionAspectClass);This will encrypt your root device volume with the CMK, and every other volume you can provision using the
Volumeconstruct which does take custom encryption key. The concept of overriding properties in form of Escape Hatches is also explained here. You can check it out for more details. Hope it helps.
This is actually not working for me @hgrgic . When I synthethize I got the following CFN template:
BlockDeviceMappings:
"0":
Ebs:
Encrypted: true
KmsKeyId: xxxx-id-xxxxxxx
And this error when deploying:
Value of property BlockDeviceMappings must be of type List
Finally I could make it working doing this:
public void newkms(IConstruct node, IKey kmsKeyId) {
if (node != null) {
CfnInstance cfnInstance = (CfnInstance) node.getNode().findChild("Resource");
cfnInstance.addPropertyOverride("BlockDeviceMappings",Arrays.asList(java.util.Map.of("DeviceName", "/dev/sda1", "Ebs", java.util.Map.of("Encrypted", "true","KmsKeyId",kmsKeyId.getKeyId()))));
}
and
IKey kmsKeyId = software.amazon.awscdk.services.kms.Key.fromKeyArn(this,"ec2-key","arn:aws:kms:us-east-1:xxxxxxxxxxx:key/key_id");
final Instance ec2 = Instance.Builder.create(this, "ec2")
.vpc(vpc)
.instanceType(InstanceType.of(InstanceClass.BURSTABLE3,InstanceSize.XLARGE2))
.machineImage(MachineImage.latestWindows(WindowsVersion.WINDOWS_SERVER_2019_ENGLISH_FULL_BASE))
.build();
new BoundaryAspects().newkms(ec2,kmsKeyId);
Just following up, we had https://github.com/aws/aws-cdk/pull/12842 which seems like it was pretty close to ready if anyone is interested in picking this up again.
Hi @ccfife,
Thanks for opening this issue! After reviewing the details, we found some other discussions that may be closely related:
Related Issues
-
#18309: Both issues address the same functionality gap - adding support for specifying KMS keys for EBS volume encryption in EC2 constructs. While the current issue requests "Support encrypted EBS volumes for EC2 instances" and mentions "BlockDeviceMappings→ebs→ Encrypted/KmsKeyId", issue #18309 specifically targets adding the KmsKeyId property to launchtemplate's blockdevicemapping ebs configuration. They both identify the same limitation in the CDK's ability to specify custom encryption keys for EBS volumes.
-
#6945: This issue focuses on the BastionHostLinux construct specifically not providing EBS volume encryption options, while the current issue requests general support for encrypted EBS volumes across EC2 instances. They share the same underlying problem - CDK constructs lacking the ability to configure encryption for EBS volumes to meet security compliance requirements for sensitive data.
-
#12049: This issue deals with encrypting EBS volumes in Auto Scaling Groups (ASGs), while the current issue addresses EC2 instances directly. They both identify the same gap in CDK functionality - the inability to properly configure encrypted EBS volumes for EC2 resources. Both issues aim to secure sensitive data through volume encryption, just in different EC2-related constructs.
-
#34491: While the current issue requests adding support for encrypted EBS volumes, this issue reveals an implementation problem where EC2 instances with encrypted EBS volumes in Auto Scaling Groups fail to launch due to KMS key permissions. They represent different aspects of the same functionality area - the first requesting the feature and the second identifying challenges in the actual implementation of EBS encryption in CDK.
This message was generated automatically to help connect related conversations and improve discoverability
Please react with 👍 or 👎 to let us know if this response was helpful!
Thank you for helping improve CDK! 🙌
Duplicate of https://github.com/aws/aws-cdk/issues/18309 and solved in https://github.com/aws/aws-cdk/pull/18326
Comments on closed issues and PRs are hard for our team to see. If you need help, please open a new issue that references this one.