Skip to content

Commit 6b73d1d

Browse files
authored
feat(ec2): add support for al2022 and amzn2 with kernel 5.x (#18117)
This PR is aimed to expand CDK ability to support `al2022` and `amzn2` with kernel 5.x ``` "/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-5.10-arm64" "/aws/service/ami-amazon-linux-latest/al2022-ami-minimal-kernel-5.10-arm64" "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-gp2" "/aws/service/ami-amazon-linux-latest/amzn-ami-hvm-x86_64-s3" "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-hvm-x86_64-s3" "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-arm64-gp2" "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2" "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-5.10-x86_64" "/aws/service/ami-amazon-linux-latest/al2022-ami-minimal-kernel-5.10-x86_64" "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-hvm-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-pv-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/amzn-ami-minimal-pv-x86_64-s3" "/aws/service/ami-amazon-linux-latest/amzn-ami-pv-x86_64-ebs" "/aws/service/ami-amazon-linux-latest/amzn-ami-pv-x86_64-s3" "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-arm64-gp2" "/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2" "/aws/service/ami-amazon-linux-latest/amzn2-ami-minimal-hvm-arm64-ebs" "/aws/service/ami-amazon-linux-latest/amzn2-ami-minimal-hvm-x86_64-ebs" ``` ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 50637e0 commit 6b73d1d

File tree

4 files changed

+171
-6
lines changed

4 files changed

+171
-6
lines changed

packages/@aws-cdk/aws-ec2/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,46 @@ You can use the `Instance` class to start up a single EC2 instance. For producti
854854
you use an `AutoScalingGroup` from the `aws-autoscaling` module instead, as AutoScalingGroups will take
855855
care of restarting your instance if it ever fails.
856856

857+
```ts
858+
declare const vpc: ec2.Vpc;
859+
declare const instanceType: ec2.InstanceType;
860+
861+
// AWS Linux
862+
new ec2.Instance(this, 'Instance1', {
863+
vpc,
864+
instanceType,
865+
machineImage: new ec2.AmazonLinuxImage(),
866+
});
867+
868+
// AWS Linux 2
869+
new ec2.Instance(this, 'Instance2', {
870+
vpc,
871+
instanceType,
872+
machineImage: new ec2.AmazonLinuxImage({
873+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
874+
}),
875+
});
876+
877+
// AWS Linux 2 with kernel 5.x
878+
new ec2.Instance(this, 'Instance3', {
879+
vpc,
880+
instanceType,
881+
machineImage: new ec2.AmazonLinuxImage({
882+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
883+
kernel: ec2.AmazonLinuxKernel.KERNEL5_X,
884+
}),
885+
});
886+
887+
// AWS Linux 2022
888+
new ec2.Instance(this, 'Instance4', {
889+
vpc,
890+
instanceType,
891+
machineImage: new ec2.AmazonLinuxImage({
892+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2022,
893+
}),
894+
});
895+
```
896+
857897
### Configuring Instances using CloudFormation Init (cfn-init)
858898

859899
CloudFormation Init allows you to configure your instances by writing files to them, installing software

packages/@aws-cdk/aws-ec2/lib/machine-image.ts

+43-5
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,13 @@ export interface AmazonLinuxImageProps {
309309
*/
310310
readonly edition?: AmazonLinuxEdition;
311311

312+
/**
313+
* What kernel version of Amazon Linux to use
314+
*
315+
* @default -
316+
*/
317+
readonly kernel?: AmazonLinuxKernel;
318+
312319
/**
313320
* Virtualization type
314321
*
@@ -376,13 +383,29 @@ export class AmazonLinuxImage extends GenericSSMParameterImage {
376383
public static ssmParameterName(props: AmazonLinuxImageProps = {}) {
377384
const generation = (props && props.generation) || AmazonLinuxGeneration.AMAZON_LINUX;
378385
const edition = (props && props.edition) || AmazonLinuxEdition.STANDARD;
379-
const virtualization = (props && props.virtualization) || AmazonLinuxVirt.HVM;
380-
const storage = (props && props.storage) || AmazonLinuxStorage.GENERAL_PURPOSE;
381386
const cpu = (props && props.cpuType) || AmazonLinuxCpuType.X86_64;
387+
let kernel = (props && props.kernel) || undefined;
388+
let virtualization: AmazonLinuxVirt | undefined;
389+
let storage: AmazonLinuxStorage | undefined;
390+
391+
if (generation === AmazonLinuxGeneration.AMAZON_LINUX_2022) {
392+
kernel = AmazonLinuxKernel.KERNEL5_X;
393+
if (props && props.storage) {
394+
throw new Error('Storage parameter does not exist in smm parameter name for Amazon Linux 2022.');
395+
}
396+
if (props && props.virtualization) {
397+
throw new Error('Virtualization parameter does not exist in smm parameter name for Amazon Linux 2022.');
398+
}
399+
} else {
400+
virtualization = (props && props.virtualization) || AmazonLinuxVirt.HVM;
401+
storage = (props && props.storage) || AmazonLinuxStorage.GENERAL_PURPOSE;
402+
}
403+
382404
const parts: Array<string|undefined> = [
383405
generation,
384406
'ami',
385407
edition !== AmazonLinuxEdition.STANDARD ? edition : undefined,
408+
kernel,
386409
virtualization,
387410
cpu,
388411
storage,
@@ -427,6 +450,21 @@ export enum AmazonLinuxGeneration {
427450
* Amazon Linux 2
428451
*/
429452
AMAZON_LINUX_2 = 'amzn2',
453+
454+
/**
455+
* Amazon Linux 2022
456+
*/
457+
AMAZON_LINUX_2022 = 'al2022',
458+
}
459+
460+
/**
461+
* Amazon Linux Kernel
462+
*/
463+
export enum AmazonLinuxKernel {
464+
/**
465+
* Standard edition
466+
*/
467+
KERNEL5_X = 'kernel-5.10',
430468
}
431469

432470
/**
@@ -441,7 +479,7 @@ export enum AmazonLinuxEdition {
441479
/**
442480
* Minimal edition
443481
*/
444-
MINIMAL = 'minimal'
482+
MINIMAL = 'minimal',
445483
}
446484

447485
/**
@@ -456,7 +494,7 @@ export enum AmazonLinuxVirt {
456494
/**
457495
* PV virtualization
458496
*/
459-
PV = 'pv'
497+
PV = 'pv',
460498
}
461499

462500
export enum AmazonLinuxStorage {
@@ -468,7 +506,7 @@ export enum AmazonLinuxStorage {
468506
/**
469507
* S3-backed storage
470508
*/
471-
S3 = 'ebs',
509+
S3 = 's3',
472510

473511
/**
474512
* General Purpose-based storage (recommended)

packages/@aws-cdk/aws-ec2/test/example.images.lit.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const amznLinux = ec2.MachineImage.latestAmazonLinux({
1515
const windows = ec2.MachineImage.latestWindows(ec2.WindowsVersion.WINDOWS_SERVER_2019_ENGLISH_FULL_BASE);
1616

1717
// Read AMI id from SSM parameter store
18-
const ssm = ec2.MachineImage.fromSSMParameter('/my/ami', ec2.OperatingSystemType.LINUX);
18+
const ssm = ec2.MachineImage.fromSsmParameter('/my/ami', { os: ec2.OperatingSystemType.LINUX });
1919

2020
// Look up the most recent image matching a set of AMI filters.
2121
// In this case, look up the NAT instance AMI, by using a wildcard

packages/@aws-cdk/aws-ec2/test/machine-image.test.ts

+87
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,93 @@ test('cached lookups of Amazon Linux', () => {
177177
]);
178178
});
179179

180+
test('cached lookups of Amazon Linux 2', () => {
181+
// WHEN
182+
const ami = ec2.MachineImage.latestAmazonLinux({
183+
cachedInContext: true,
184+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
185+
}).getImage(stack).imageId;
186+
187+
// THEN
188+
expect(ami).toEqual('dummy-value-for-/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2');
189+
expect(app.synth().manifest.missing).toEqual([
190+
{
191+
key: 'ssm:account=1234:parameterName=/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2:region=testregion',
192+
props: {
193+
account: '1234',
194+
region: 'testregion',
195+
parameterName: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2',
196+
},
197+
provider: 'ssm',
198+
},
199+
]);
200+
});
201+
202+
test('cached lookups of Amazon Linux 2 with kernel 5.x', () => {
203+
// WHEN
204+
const ami = ec2.MachineImage.latestAmazonLinux({
205+
cachedInContext: true,
206+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2,
207+
kernel: ec2.AmazonLinuxKernel.KERNEL5_X,
208+
}).getImage(stack).imageId;
209+
210+
// THEN
211+
expect(ami).toEqual('dummy-value-for-/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2');
212+
expect(app.synth().manifest.missing).toEqual([
213+
{
214+
key: 'ssm:account=1234:parameterName=/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2:region=testregion',
215+
props: {
216+
account: '1234',
217+
region: 'testregion',
218+
parameterName: '/aws/service/ami-amazon-linux-latest/amzn2-ami-kernel-5.10-hvm-x86_64-gp2',
219+
},
220+
provider: 'ssm',
221+
},
222+
]);
223+
});
224+
225+
test('throw error if storage param is set for Amazon Linux 2022', () => {
226+
expect(() => {
227+
ec2.MachineImage.latestAmazonLinux({
228+
cachedInContext: true,
229+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2022,
230+
storage: ec2.AmazonLinuxStorage.GENERAL_PURPOSE,
231+
}).getImage(stack).imageId;
232+
}).toThrow(/Storage parameter does not exist in smm parameter name for Amazon Linux 2022./);
233+
});
234+
235+
test('throw error if virtualization param is set for Amazon Linux 2022', () => {
236+
expect(() => {
237+
ec2.MachineImage.latestAmazonLinux({
238+
cachedInContext: true,
239+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2022,
240+
virtualization: ec2.AmazonLinuxVirt.HVM,
241+
}).getImage(stack).imageId;
242+
}).toThrow(/Virtualization parameter does not exist in smm parameter name for Amazon Linux 2022./);
243+
});
244+
245+
test('cached lookups of Amazon Linux 2022 with kernel 5.x', () => {
246+
// WHEN
247+
const ami = ec2.MachineImage.latestAmazonLinux({
248+
cachedInContext: true,
249+
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX_2022,
250+
}).getImage(stack).imageId;
251+
252+
// THEN
253+
expect(ami).toEqual('dummy-value-for-/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-5.10-x86_64');
254+
expect(app.synth().manifest.missing).toEqual([
255+
{
256+
key: 'ssm:account=1234:parameterName=/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-5.10-x86_64:region=testregion',
257+
props: {
258+
account: '1234',
259+
region: 'testregion',
260+
parameterName: '/aws/service/ami-amazon-linux-latest/al2022-ami-kernel-5.10-x86_64',
261+
},
262+
provider: 'ssm',
263+
},
264+
]);
265+
});
266+
180267
function isWindowsUserData(ud: ec2.UserData) {
181268
return ud.render().indexOf('powershell') > -1;
182269
}

0 commit comments

Comments
 (0)