Skip to content

Commit 22fc8b9

Browse files
authored
feat(aws-eks): add construct library for EKS (#1655)
Construct library to set up an EKS cluster and add nodes to it. Generalizes adding AutoScalingGroup capacity and make it the same between both ECS and EKS clusters. Fixes naming inconsistencies among properties of `AutoScalingGroup`, and between EC2 AutoScaling and Application AutoScaling. This PR takes #991 but reimplements the API in the style of the ECS library. BREAKING CHANGE: For `AutoScalingGroup`, renamed `minSize` => `minCapacity`, `maxSize` => `maxCapacity`, for consistency with `desiredCapacity` and also Application AutoScaling. For ECS's `addDefaultAutoScalingGroupCapacity()`, `instanceCount` => `desiredCapacity` and the function now takes an ID (pass `"DefaultAutoScalingGroup"` to avoid interruption to your deployments).
1 parent 7b2bf13 commit 22fc8b9

25 files changed

+1934
-109
lines changed

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,25 @@ import { BaseTargetTrackingProps, PredefinedMetric, TargetTrackingScalingPolicy
1818
const NAME_TAG: string = 'Name';
1919

2020
/**
21-
* Properties of a Fleet
21+
* Basic properties of an AutoScalingGroup, except the exact machines to run and where they should run
22+
*
23+
* Constructs that want to create AutoScalingGroups can inherit
24+
* this interface and specialize the essential parts in various ways.
2225
*/
23-
export interface AutoScalingGroupProps {
24-
/**
25-
* Type of instance to launch
26-
*/
27-
instanceType: ec2.InstanceType;
28-
26+
export interface CommonAutoScalingGroupProps {
2927
/**
3028
* Minimum number of instances in the fleet
3129
*
3230
* @default 1
3331
*/
34-
minSize?: number;
32+
minCapacity?: number;
3533

3634
/**
3735
* Maximum number of instances in the fleet
3836
*
3937
* @default desiredCapacity
4038
*/
41-
maxSize?: number;
39+
maxCapacity?: number;
4240

4341
/**
4442
* Initial amount of instances in the fleet
@@ -52,16 +50,6 @@ export interface AutoScalingGroupProps {
5250
*/
5351
keyName?: string;
5452

55-
/**
56-
* AMI to launch
57-
*/
58-
machineImage: ec2.IMachineImageSource;
59-
60-
/**
61-
* VPC to launch these instances in.
62-
*/
63-
vpc: ec2.IVpcNetwork;
64-
6553
/**
6654
* Where to place instances within the VPC
6755
*/
@@ -153,6 +141,26 @@ export interface AutoScalingGroupProps {
153141
associatePublicIpAddress?: boolean;
154142
}
155143

144+
/**
145+
* Properties of a Fleet
146+
*/
147+
export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
148+
/**
149+
* VPC to launch these instances in.
150+
*/
151+
vpc: ec2.IVpcNetwork;
152+
153+
/**
154+
* Type of instance to launch
155+
*/
156+
instanceType: ec2.InstanceType;
157+
158+
/**
159+
* AMI to launch
160+
*/
161+
machineImage: ec2.IMachineImageSource;
162+
}
163+
156164
/**
157165
* A Fleet represents a managed set of EC2 instances
158166
*
@@ -236,19 +244,19 @@ export class AutoScalingGroup extends cdk.Construct implements IAutoScalingGroup
236244

237245
const desiredCapacity =
238246
(props.desiredCapacity !== undefined ? props.desiredCapacity :
239-
(props.minSize !== undefined ? props.minSize :
240-
(props.maxSize !== undefined ? props.maxSize : 1)));
241-
const minSize = props.minSize !== undefined ? props.minSize : 1;
242-
const maxSize = props.maxSize !== undefined ? props.maxSize : desiredCapacity;
247+
(props.minCapacity !== undefined ? props.minCapacity :
248+
(props.maxCapacity !== undefined ? props.maxCapacity : 1)));
249+
const minCapacity = props.minCapacity !== undefined ? props.minCapacity : 1;
250+
const maxCapacity = props.maxCapacity !== undefined ? props.maxCapacity : desiredCapacity;
243251

244-
if (desiredCapacity < minSize || desiredCapacity > maxSize) {
245-
throw new Error(`Should have minSize (${minSize}) <= desiredCapacity (${desiredCapacity}) <= maxSize (${maxSize})`);
252+
if (desiredCapacity < minCapacity || desiredCapacity > maxCapacity) {
253+
throw new Error(`Should have minCapacity (${minCapacity}) <= desiredCapacity (${desiredCapacity}) <= maxCapacity (${maxCapacity})`);
246254
}
247255

248256
const asgProps: CfnAutoScalingGroupProps = {
249257
cooldown: props.cooldownSeconds !== undefined ? `${props.cooldownSeconds}` : undefined,
250-
minSize: minSize.toString(),
251-
maxSize: maxSize.toString(),
258+
minSize: minCapacity.toString(),
259+
maxSize: maxCapacity.toString(),
252260
desiredCapacity: desiredCapacity.toString(),
253261
launchConfigurationName: launchConfig.ref,
254262
loadBalancerNames: new cdk.Token(() => this.loadBalancerNames.length > 0 ? this.loadBalancerNames : undefined),

packages/@aws-cdk/aws-autoscaling/test/test.auto-scaling-group.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,16 @@ export = {
126126
test.done();
127127
},
128128

129-
'can set minSize, maxSize, desiredCapacity to 0'(test: Test) {
129+
'can set minCapacity, maxCapacity, desiredCapacity to 0'(test: Test) {
130130
const stack = new cdk.Stack(undefined, 'MyStack', { env: { region: 'us-east-1', account: '1234' }});
131131
const vpc = mockVpc(stack);
132132

133133
new autoscaling.AutoScalingGroup(stack, 'MyFleet', {
134134
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
135135
machineImage: new ec2.AmazonLinuxImage(),
136136
vpc,
137-
minSize: 0,
138-
maxSize: 0,
137+
minCapacity: 0,
138+
maxCapacity: 0,
139139
desiredCapacity: 0
140140
});
141141

@@ -159,7 +159,7 @@ export = {
159159
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
160160
machineImage: new ec2.AmazonLinuxImage(),
161161
vpc,
162-
minSize: 10
162+
minCapacity: 10
163163
});
164164

165165
// THEN
@@ -183,7 +183,7 @@ export = {
183183
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
184184
machineImage: new ec2.AmazonLinuxImage(),
185185
vpc,
186-
maxSize: 10
186+
maxCapacity: 10
187187
});
188188

189189
// THEN
@@ -415,8 +415,8 @@ export = {
415415
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
416416
machineImage: new ec2.AmazonLinuxImage(),
417417
vpc,
418-
minSize: 0,
419-
maxSize: 0,
418+
minCapacity: 0,
419+
maxCapacity: 0,
420420
desiredCapacity: 0,
421421
associatePublicIpAddress: true,
422422
});
@@ -438,8 +438,8 @@ export = {
438438
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
439439
machineImage: new ec2.AmazonLinuxImage(),
440440
vpc,
441-
minSize: 0,
442-
maxSize: 0,
441+
minCapacity: 0,
442+
maxCapacity: 0,
443443
desiredCapacity: 0,
444444
associatePublicIpAddress: false,
445445
});
@@ -461,8 +461,8 @@ export = {
461461
instanceType: new ec2.InstanceTypePair(ec2.InstanceClass.M4, ec2.InstanceSize.Micro),
462462
machineImage: new ec2.AmazonLinuxImage(),
463463
vpc,
464-
minSize: 0,
465-
maxSize: 0,
464+
minCapacity: 0,
465+
maxCapacity: 0,
466466
desiredCapacity: 0,
467467
});
468468

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class GenericLinuxImage implements IMachineImageSource {
190190
public getImage(scope: Construct): MachineImage {
191191
const stack = Stack.find(scope);
192192
const region = stack.requireRegion('AMI cannot be determined');
193-
const ami = this.amiMap[region];
193+
const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
194194
if (!ami) {
195195
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
196196
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const cluster = new ecs.Cluster(this, 'Cluster', {
2121
});
2222

2323
// Add capacity to it
24-
cluster.addDefaultAutoScalingGroupCapacity({
24+
cluster.addDefaultAutoScalingGroupCapacity('Capacity', {
2525
instanceType: new ec2.InstanceType("t2.xlarge"),
2626
instanceCount: 3,
2727
});

packages/@aws-cdk/aws-ecs/lib/cluster.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,13 @@ export class Cluster extends cdk.Construct implements ICluster {
7474
*
7575
* Returns the AutoScalingGroup so you can add autoscaling settings to it.
7676
*/
77-
public addDefaultAutoScalingGroupCapacity(options: AddDefaultAutoScalingGroupOptions): autoscaling.AutoScalingGroup {
78-
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'DefaultAutoScalingGroup', {
77+
public addDefaultAutoScalingGroupCapacity(id: string, options: AddDefaultAutoScalingGroupOptions): autoscaling.AutoScalingGroup {
78+
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, id, {
79+
...options,
7980
vpc: this.vpc,
80-
instanceType: options.instanceType,
8181
machineImage: new EcsOptimizedAmi(),
82-
updateType: autoscaling.UpdateType.ReplacingUpdate,
83-
minSize: options.minCapacity,
84-
maxSize: options.maxCapacity,
85-
desiredCapacity: options.instanceCount,
82+
updateType: options.updateType || autoscaling.UpdateType.ReplacingUpdate,
83+
instanceType: options.instanceType,
8684
});
8785

8886
this.addAutoScalingGroupCapacity(autoScalingGroup, options);
@@ -375,31 +373,9 @@ export interface AddAutoScalingGroupCapacityOptions {
375373
/**
376374
* Properties for adding autoScalingGroup
377375
*/
378-
export interface AddDefaultAutoScalingGroupOptions extends AddAutoScalingGroupCapacityOptions {
379-
376+
export interface AddDefaultAutoScalingGroupOptions extends AddAutoScalingGroupCapacityOptions, autoscaling.CommonAutoScalingGroupProps {
380377
/**
381378
* The type of EC2 instance to launch into your Autoscaling Group
382379
*/
383380
instanceType: ec2.InstanceType;
384-
385-
/**
386-
* Number of container instances registered in your ECS Cluster
387-
*
388-
* @default 1
389-
*/
390-
instanceCount?: number;
391-
392-
/**
393-
* Maximum number of instances
394-
*
395-
* @default Same as instanceCount
396-
*/
397-
maxCapacity?: number;
398-
399-
/**
400-
* Minimum number of instances
401-
*
402-
* @default Same as instanceCount
403-
*/
404-
minCapacity?: number;
405381
}

packages/@aws-cdk/aws-ecs/test/ec2/integ.event-task.lit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class EventStack extends cdk.Stack {
1212
const vpc = new ec2.VpcNetwork(this, 'Vpc', { maxAZs: 1 });
1313

1414
const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });
15-
cluster.addDefaultAutoScalingGroupCapacity({
15+
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
1616
instanceType: new ec2.InstanceType('t2.micro')
1717
});
1818

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-awsvpc-nw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ');
1010
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });
1111

1212
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
13-
cluster.addDefaultAutoScalingGroupCapacity({
13+
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
1414
instanceType: new ec2.InstanceType('t2.micro')
1515
});
1616

packages/@aws-cdk/aws-ecs/test/ec2/integ.lb-bridge-nw.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const stack = new cdk.Stack(app, 'aws-ecs-integ-ecs');
1010
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 2 });
1111

1212
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
13-
cluster.addDefaultAutoScalingGroupCapacity({
13+
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
1414
instanceType: new ec2.InstanceType('t2.micro')
1515
});
1616

@@ -44,4 +44,4 @@ listener.addTargets('ECS', {
4444

4545
new cdk.Output(stack, 'LoadBalancerDNS', { value: lb.dnsName, });
4646

47-
app.run();
47+
app.run();

packages/@aws-cdk/aws-ecs/test/ec2/test.ec2-event-rule-target.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export = {
1111
const stack = new cdk.Stack();
1212
const vpc = new ec2.VpcNetwork(stack, 'Vpc', { maxAZs: 1 });
1313
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
14-
cluster.addDefaultAutoScalingGroupCapacity({
14+
cluster.addDefaultAutoScalingGroupCapacity('DefaultAutoScalingGroup', {
1515
instanceType: new ec2.InstanceType('t2.micro')
1616
});
1717

@@ -58,4 +58,4 @@ export = {
5858

5959
test.done();
6060
}
61-
};
61+
};

0 commit comments

Comments
 (0)