Skip to content

Commit

Permalink
fix(aws-ecs): add ASG capacity via Capacity Provider by not specifyin…
Browse files Browse the repository at this point in the history
…g machineImageType (#16361)

fix(aws-ecs): make `Cluster.addAsgCapacityProvider()` not need specify `machineImageType`

close #16360 
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
neilkuan authored Oct 12, 2021
1 parent 4300a30 commit 93b3fdc
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 4 deletions.
11 changes: 9 additions & 2 deletions packages/@aws-cdk/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,15 @@ 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= {}) {
// 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,
});
Expand Down Expand Up @@ -1062,6 +1062,11 @@ export class AsgCapacityProvider extends CoreConstruct {
*/
readonly autoScalingGroup: autoscaling.AutoScalingGroup;

/**
* Auto Scaling Group machineImageType.
*/
readonly machineImageType: MachineImageType;

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

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

this.machineImageType = props.machineImageType ?? MachineImageType.AMAZON_LINUX_2;

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

Expand Down
104 changes: 104 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,107 @@ describe('cluster', () => {

});
});

test('can add ASG capacity via Capacity Provider by not specifying machineImageType', () => {
// 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: [],
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import * as ecr_assets from '@aws-cdk/aws-ecr-assets';
import * as s3 from '@aws-cdk/aws-s3';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import * as ssm from '@aws-cdk/aws-ssm';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as ecs from '../lib';

describe('container definition', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { Repository } from '@aws-cdk/aws-ecr';
import * as iam from '@aws-cdk/aws-iam';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import * as ssm from '@aws-cdk/aws-ssm';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as cdk from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { testFutureBehavior } from '@aws-cdk/cdk-build-tools/lib/feature-flag';
import * as ecs from '../../lib';

describe('ec2 task definition', () => {
Expand Down

0 comments on commit 93b3fdc

Please sign in to comment.