Skip to content
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

Merged
merged 15 commits into from
Jul 25, 2019
30 changes: 26 additions & 4 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, the generation and windowsVersion properties would be merged, but I didn't think it was worth the refactoring and breaking change.

Copy link
Contributor

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 of amazonLinuxGeneration (still support generation for backwards compat and in the next MV we will remove)

Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

Copy link
Contributor

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?


/**
* The ECS-optimized AMI variant to use.
*
Expand All @@ -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;
Expand All @@ -267,9 +280,17 @@ export class EcsOptimizedAmi implements ec2.IMachineImage {
if (props && props.generation) { // generation defined in the props object
if (props.generation === ec2.AmazonLinuxGeneration.AMAZON_LINUX && this.hwType !== AmiHardwareType.STANDARD) {
throw new Error(`Amazon Linux does not support special hardware type. Use Amazon Linux 2 instead`);
} else if (props.windowsVersion) {
throw new Error('"windowsVersion" and Linux image "generation" cannot be both set');
} 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');
} else {
this.windowsVersion = props.windowsVersion;
}
} else { // generation not defined in props object
// always default to Amazon Linux v2 regardless of HW
this.generation = ec2.AmazonLinuxGeneration.AMAZON_LINUX_2;
Expand All @@ -279,6 +300,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";
Expand All @@ -291,7 +313,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
};
}
}
Expand Down Expand Up @@ -510,7 +532,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',

Expand Down
82 changes: 81 additions & 1 deletion packages/@aws-cdk/aws-ecs/test/test.ecs-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import cloudmap = require('@aws-cdk/aws-servicediscovery');
import cdk = require('@aws-cdk/core');
import { Test } from 'nodeunit';
import ecs = require('../lib');
import { App } from '@aws-cdk/core';

export = {
"When creating an ECS Cluster": {
Expand Down Expand Up @@ -237,7 +238,8 @@ export = {

"allows specifying special HW AMI Type"(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const app = new App();
const stack = new cdk.Stack(app, 'test');
const vpc = new ec2.Vpc(stack, 'MyVpc', {});

const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
Expand All @@ -249,12 +251,21 @@ export = {
});

// THEN
const assembly = app.synth();
const template = assembly.getStack(stack.stackName).template;
expect(stack).to(haveResource("AWS::AutoScaling::LaunchConfiguration", {
ImageId: {
Ref: "SsmParameterValueawsserviceecsoptimizedamiamazonlinux2gpurecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter"
}
}));

test.deepEqual(template.Parameters, {
"SsmParameterValueawsserviceecsoptimizedamiamazonlinux2gpurecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/amazon-linux-2/gpu/recommended/image_id"
}
});

test.done();
},

Expand All @@ -274,8 +285,77 @@ export = {
hardwareType: ecs.AmiHardwareType.GPU,
}),
});
}, /Amazon Linux does not support special hardware type/);

test.done();
},

"allows specifying windows image"(test: Test) {
// GIVEN
const app = new App();
const stack = new cdk.Stack(app, 'test');
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
const assembly = app.synth();
const template = assembly.getStack(stack.stackName).template;
test.deepEqual(template.Parameters, {
"SsmParameterValueawsserviceecsoptimizedamiwindowsserver2019englishfullrecommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/ecs/optimized-ami/windows_server/2019/english/full/recommended/image_id"
}
});

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();
},

"errors if windowsVersion and linux generation are set"(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('WindowsScalingGroup', {
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: new ecs.EcsOptimizedAmi({
windowsVersion: ecs.WindowsOptimizedVersion.SERVER_2019,
generation: ec2.AmazonLinuxGeneration.AMAZON_LINUX
}),
});
}, /"windowsVersion" and Linux image "generation" cannot be both set/);

test.done();
},

Expand Down