-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ecs): ECS optimized Windows images #3376
Changes from 1 commit
eb63ce4
48780fb
47e81a6
e3e5739
5c01a39
b1f003b
a95a33f
f2a8e1d
0309b87
9bc06bf
12d99eb
450b507
42bfb0b
3cbeb5f
19e874d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -231,6 +231,11 @@ export class Cluster extends Resource implements ICluster { | |
} | ||
} | ||
|
||
export enum WindowsOptimizedVersion { | ||
SERVER_2019 = '2019', | ||
SERVER_2016 = '2016', | ||
} | ||
|
||
/** | ||
* The properties that define which ECS-optimized AMI is used. | ||
*/ | ||
|
@@ -242,6 +247,13 @@ export interface EcsOptimizedAmiProps { | |
*/ | ||
readonly generation?: ec2.AmazonLinuxGeneration; | ||
|
||
/** | ||
* The Windows Server version to use. | ||
* | ||
* @default none, uses Linux generation | ||
*/ | ||
readonly windowsVersion?: WindowsOptimizedVersion; | ||
|
||
/** | ||
* The ECS-optimized AMI variant to use. | ||
* | ||
|
@@ -251,10 +263,11 @@ export interface EcsOptimizedAmiProps { | |
} | ||
|
||
/** | ||
* Construct a Linux machine image from the latest ECS Optimized AMI published in SSM | ||
* Construct a Linux or Windows machine image from the latest ECS Optimized AMI published in SSM | ||
*/ | ||
export class EcsOptimizedAmi implements ec2.IMachineImage { | ||
private readonly generation: ec2.AmazonLinuxGeneration; | ||
private readonly generation?: ec2.AmazonLinuxGeneration; | ||
private readonly windowsVersion?: WindowsOptimizedVersion; | ||
private readonly hwType: AmiHardwareType; | ||
|
||
private readonly amiParameterName: string; | ||
|
@@ -270,6 +283,10 @@ export class EcsOptimizedAmi implements ec2.IMachineImage { | |
} else { | ||
this.generation = props.generation; | ||
} | ||
} else if (props && props.windowsVersion) { | ||
if (this.hwType !== AmiHardwareType.STANDARD) { | ||
throw new Error('Windows Server does not support special hardware type. Use Amazon Linux 2 instead'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove "use Amazon Linux 2 instead". I am assuming that if a user explicitly specified they wanted Windows, they should probably just not specify the HW type and not use Linux instead... :-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
} | ||
} else { // generation not defined in props object | ||
// always default to Amazon Linux v2 regardless of HW | ||
this.generation = ec2.AmazonLinuxGeneration.AMAZON_LINUX_2; | ||
|
@@ -279,6 +296,7 @@ export class EcsOptimizedAmi implements ec2.IMachineImage { | |
this.amiParameterName = "/aws/service/ecs/optimized-ami/" | ||
+ ( this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX ? "amazon-linux/" : "" ) | ||
+ ( this.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX_2 ? "amazon-linux-2/" : "" ) | ||
+ ( this.windowsVersion ? `windows_server/${this.windowsVersion}/english/full/` : "" ) | ||
+ ( this.hwType === AmiHardwareType.GPU ? "gpu/" : "" ) | ||
+ ( this.hwType === AmiHardwareType.ARM ? "arm64/" : "" ) | ||
+ "recommended/image_id"; | ||
|
@@ -291,7 +309,7 @@ export class EcsOptimizedAmi implements ec2.IMachineImage { | |
const ami = ssm.StringParameter.valueForStringParameter(scope, this.amiParameterName); | ||
return { | ||
imageId: ami, | ||
osType: ec2.OperatingSystemType.LINUX | ||
osType: this.windowsVersion ? ec2.OperatingSystemType.WINDOWS : ec2.OperatingSystemType.LINUX | ||
}; | ||
} | ||
} | ||
|
@@ -510,7 +528,7 @@ export interface CloudMapNamespaceOptions { | |
export enum AmiHardwareType { | ||
|
||
/** | ||
* Use the Amazon ECS-optimized Amazon Linux 2 AMI. | ||
* Use the standard Amazon ECS-optimized AMI. | ||
*/ | ||
STANDARD = 'Standard', | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -274,8 +274,52 @@ export = { | |||||||||||||||||||||
hardwareType: ecs.AmiHardwareType.GPU, | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
}, /Amazon Linux does not support special hardware type/); | ||||||||||||||||||||||
|
||||||||||||||||||||||
test.done(); | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
|
||||||||||||||||||||||
"allows specifying windows image"(test: Test) { | ||||||||||||||||||||||
// GIVEN | ||||||||||||||||||||||
const stack = new cdk.Stack(); | ||||||||||||||||||||||
const vpc = new ec2.Vpc(stack, 'MyVpc', {}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); | ||||||||||||||||||||||
cluster.addCapacity('WindowsAutoScalingGroup', { | ||||||||||||||||||||||
instanceType: new ec2.InstanceType('t2.micro'), | ||||||||||||||||||||||
machineImage: new ecs.EcsOptimizedAmi({ | ||||||||||||||||||||||
windowsVersion: ecs.WindowsOptimizedVersion.SERVER_2019, | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// THEN | ||||||||||||||||||||||
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", { | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add an expectation on the parameter with the desired SSM path... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I don't understand your comment. I reused the existing HW AMI Type test: aws-cdk/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts Lines 252 to 256 in 47e81a6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This just shows the ImageId is a reference to a synthesized CloudFormation parameter that is added to your template. You should write your expectation against the default value of the parameter which will contain the SSM parameter name that you want. What I usually do is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good call, I was missing an assignation, thanks. I have an issue with the matching now. I tried changing the Should I use an integration test instead, to match the whole stack? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I think there's a bug in the matcher (raise an issue?). What you could do in the meantime is as follows; // you'll need an "App"
const app = new App();
const stack = new Stack(app, 'test');
// ...
// then
const assembly = app.synth();
const template = assembly.getStack(stack.stackName).template;
test.deepEqual(template.Parameters, { /* expectation here */ }); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alright, thanks a lot. Should I also update the existing Linux AMI test? aws-cdk/packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts Lines 253 to 257 in 5c01a39
If so, should I do it in another PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to add to this PR There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks again for the help! |
||||||||||||||||||||||
ImageId: { | ||||||||||||||||||||||
Ref: "SsmParameterValueawsserviceecsoptimizedamirecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter" | ||||||||||||||||||||||
} | ||||||||||||||||||||||
})); | ||||||||||||||||||||||
|
||||||||||||||||||||||
test.done(); | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
|
||||||||||||||||||||||
"errors if windows given with special HW type"(test: Test) { | ||||||||||||||||||||||
// GIVEN | ||||||||||||||||||||||
const stack = new cdk.Stack(); | ||||||||||||||||||||||
const vpc = new ec2.Vpc(stack, 'MyVpc', {}); | ||||||||||||||||||||||
|
||||||||||||||||||||||
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); | ||||||||||||||||||||||
|
||||||||||||||||||||||
// THEN | ||||||||||||||||||||||
test.throws(() => { | ||||||||||||||||||||||
cluster.addCapacity('WindowsGpuAutoScalingGroup', { | ||||||||||||||||||||||
instanceType: new ec2.InstanceType('t2.micro'), | ||||||||||||||||||||||
machineImage: new ecs.EcsOptimizedAmi({ | ||||||||||||||||||||||
windowsVersion: ecs.WindowsOptimizedVersion.SERVER_2019, | ||||||||||||||||||||||
hardwareType: ecs.AmiHardwareType.GPU, | ||||||||||||||||||||||
}), | ||||||||||||||||||||||
}); | ||||||||||||||||||||||
}, /Windows Server does not support special hardware type/); | ||||||||||||||||||||||
|
||||||||||||||||||||||
test.done(); | ||||||||||||||||||||||
}, | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, the
generation
andwindowsVersion
properties would be merged, but I didn't think it was worth the refactoring and breaking change.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can @deprecate
generation
in favor ofamazonLinuxGeneration
(still supportgeneration
for backwards compat and in the next MV we will remove)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts? Maybe in a subsequent PR? At least update #3398 with this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't see your previous comment. I can add it to this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we just replace it with an
image
union construct, to have the definition enforce the mutual exclusion?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So what's up with this? Didn't we say this is gone?