How do I trigger AWS to create an AWS managed key for EBS in CDK Code?

0

Use AWS CDK to create an EBS volume, configure KMS encryption, and use a KMS key managed by AWS,Key Alias: aws/ebs.

When I deploy it in a region where I haven't used EBS services before, I get error Could not find any key with alias named alias/aws/ebs.

Can I do something in the cdk code other than manually using EBS services once in the AWS console to let AWS create the ebs kms key?

------------The following was edited in 20240329

Current use case is in imagebuilder.CfnImageRecipe, as follows, if I only set encrypted:true and do not set kmsKeyId, it will show unencrypted at the end. Both properties need to be set to be configured for encryption.

    this.imageRecipe = new imagebuilder.CfnImageRecipe(this, 'DemoImageRecipe'{
      ...
      blockDeviceMappings: [
        {
          deviceName: '/dev/sda1',
          ebs: {
            encrypted: true,
            kmsKeyId: props.KMSKeyId,
            deleteOnTermination: true,
            iops: 150,
            volumeSize: 50,
            volumeType: 'gp3',
            throughput: 125,
          },
        },
      ],
      ...
    });
tong_K
已提問 1 個月前檢視次數 396 次
2 個答案
1

CDK does not provide a direct method to enable EBS encryption by default at the account or region level. This functionality is typically managed through the AWS Management Console or AWS CLI. After enabling encryption by default through AWS CLI or Console, all new EBS volumes and snapshots in the specified region are encrypted under the AWS managed key, unless you specify otherwise in your CDK code or other AWS service configurations.

https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_EnableEbsEncryptionByDefault.html

    // Add an EBS volume encrypted with the default AWS managed key
    instance.addBlockDevice({
      deviceName: '/dev/sdh',
      volume: ec2.BlockDeviceVolume.ebs(20, {
        encrypted: true, // Ensures the volume is encrypted
        // Do not specify the kmsKey property to use the default AWS managed key
      }),
    });
profile picture
專家
已回答 1 個月前
profile picture
專家
Artem
已審閱 1 個月前
  • Thank you for your answer. According to your description, we need to create a EBS volume and choose to encrypt,but not specify the kmsKey property, right? But this doesn't work in my use case, I edited my question again, please check it , thank you.

0

Hello, To create an EBS volume and use AWS managed key for EBS encryption, you can modify the CDK code to use the default AWS managed key for EBS encryption. Here's how you can do it:

import * as cdk from '@aws-cdk/core'; import * as ec2 from '@aws-cdk/aws-ec2';

export class EbsWithEncryptionStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props);

// Create EBS volume with encryption using AWS managed key for EBS
const volume = new ec2.Volume(this, 'EbsVolume', {
  availabilityZone: 'us-east-1a', // Change availability zone as per your requirement
  encrypted: true, // Enable encryption
  volumeType: ec2.EbsDeviceVolumeType.GP2, // Change volume type as per your requirement
});

// Output volume ID
new cdk.CfnOutput(this, 'EbsVolumeId', {
  value: volume.volumeId,
});

} }

const app = new cdk.App(); new EbsWithEncryptionStack(app, 'EbsWithEncryptionStack');

The sample code above create an EBS volume using ec2.Volume and set encrypted property to true to enable encryption. Since we haven't specified any KMS key explicitly, AWS will use the default AWS managed key for EBS encryption automatically.

AWS
支援工程師
已回答 1 個月前
  • Thank you for your answer, then I'm very sorry I didn't describe the use case to clear me, I edited my question again, please check it again, thank you.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南