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

fix(aws-ecs): add ASG capacity via Capacity Provider by not specifying machineImageType #16361

Merged
merged 6 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,17 @@ export class Cluster extends Resource implements ICluster {
*
* @param provider the capacity provider to add to this cluster.
*/
public addAsgCapacityProvider(provider: AsgCapacityProvider, options: AddAutoScalingGroupCapacityOptions = {}) {
public addAsgCapacityProvider(provider: AsgCapacityProvider, options?: AddAutoScalingGroupCapacityOptions) {
neilkuan marked this conversation as resolved.
Show resolved Hide resolved
// Don't add the same capacity provider more than once.
if (this._capacityProviderNames.includes(provider.capacityProviderName)) {
return;
}

this._hasEc2Capacity = true;
this.configureAutoScalingGroup(provider.autoScalingGroup, {
...options,
machineImageType: provider.machineImageType,
// Don't enable the instance-draining lifecycle hook if managed termination protection is enabled
taskDrainTime: provider.enableManagedTerminationProtection ? Duration.seconds(0) : options.taskDrainTime,
taskDrainTime: provider.enableManagedTerminationProtection ? Duration.seconds(0) : options?.taskDrainTime,
});

this._capacityProviderNames.push(provider.capacityProviderName);
Expand Down Expand Up @@ -1315,6 +1315,12 @@ export class AsgCapacityProvider extends CoreConstruct {
*/
readonly autoScalingGroup: autoscaling.AutoScalingGroup;

/**
* Auto Scaling Group machineImageType.
*/
readonly machineImageType: MachineImageType
;
neilkuan marked this conversation as resolved.
Show resolved Hide resolved

/**
* Whether managed termination protection is enabled
*/
Expand All @@ -1325,6 +1331,8 @@ export class AsgCapacityProvider extends CoreConstruct {

this.autoScalingGroup = props.autoScalingGroup as autoscaling.AutoScalingGroup;

this.machineImageType = props.machineImageType!;
neilkuan marked this conversation as resolved.
Show resolved Hide resolved

this.enableManagedTerminationProtection =
props.enableManagedTerminationProtection === undefined ? true : props.enableManagedTerminationProtection;

Expand Down
105 changes: 105 additions & 0 deletions packages/@aws-cdk/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2142,3 +2142,108 @@ describe('cluster', () => {

});
});


test('can add ASG capacity via Capacity Provider not specify mahcineImageType', () => {
neilkuan marked this conversation as resolved.
Show resolved Hide resolved
// GIVEN
const app = new cdk.App();
const stack = new cdk.Stack(app, 'test');
const vpc = new ec2.Vpc(stack, 'Vpc');
const cluster = new ecs.Cluster(stack, 'EcsCluster');

const autoScalingGroupAl2 = new autoscaling.AutoScalingGroup(stack, 'asgal2', {
vpc,
instanceType: new ec2.InstanceType('bogus'),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
});

const autoScalingGroupBottlerocket = new autoscaling.AutoScalingGroup(stack, 'asgBottlerocket', {
vpc,
instanceType: new ec2.InstanceType('bogus'),
machineImage: new ecs.BottleRocketImage(),
});

// WHEN
const capacityProviderAl2 = new ecs.AsgCapacityProvider(stack, 'provideral2', {
autoScalingGroup: autoScalingGroupAl2,
enableManagedTerminationProtection: false,
});

const capacityProviderBottlerocket = new ecs.AsgCapacityProvider(stack, 'providerBottlerocket', {
autoScalingGroup: autoScalingGroupBottlerocket,
enableManagedTerminationProtection: false,
machineImageType: ecs.MachineImageType.BOTTLEROCKET,
});

cluster.enableFargateCapacityProviders();

// Ensure not added twice
cluster.addAsgCapacityProvider(capacityProviderAl2);
cluster.addAsgCapacityProvider(capacityProviderAl2);

// Add Bottlerocket ASG Capacity Provider
cluster.addAsgCapacityProvider(capacityProviderBottlerocket);


// THEN Bottlerocket LaunchConfiguration
expect(stack).toHaveResource('AWS::AutoScaling::LaunchConfiguration', {
ImageId: {
Ref: 'SsmParameterValueawsservicebottlerocketawsecs1x8664latestimageidC96584B6F00A464EAD1953AFF4B05118Parameter',

},
UserData: {
'Fn::Base64': {
'Fn::Join': [
'',
[
'\n[settings.ecs]\ncluster = \"',
{
Ref: 'EcsCluster97242B84',
},
'\"',
],
],
},
},
});

// THEN AmazonLinux2 LaunchConfiguration
expect(stack).toHaveResource('AWS::AutoScaling::LaunchConfiguration', {
ImageId: {
Ref: 'SsmParameterValueawsserviceecsoptimizedamiamazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter',
},
UserData: {
'Fn::Base64': {
'Fn::Join': [
'',
[
'#!/bin/bash\necho ECS_CLUSTER=',
{
Ref: 'EcsCluster97242B84',

},
' >> /etc/ecs/ecs.config\nsudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP\nsudo service iptables save\necho ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config',
],
],
},
},
});

expect(stack).toHaveResource('AWS::ECS::ClusterCapacityProviderAssociations', {
CapacityProviders: [
'FARGATE',
'FARGATE_SPOT',
{
Ref: 'provideral2A427CBC0',
},
{
Ref: 'providerBottlerocket90C039FA',
},
],
Cluster: {
Ref: 'EcsCluster97242B84',
},
DefaultCapacityProviderStrategy: [],
});

});