diff --git a/packages/@aws-cdk/aws-ecs/lib/cluster.ts b/packages/@aws-cdk/aws-ecs/lib/cluster.ts index cc97f4aadfafe..aae5d320b20af 100644 --- a/packages/@aws-cdk/aws-ecs/lib/cluster.ts +++ b/packages/@aws-cdk/aws-ecs/lib/cluster.ts @@ -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, }); @@ -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 */ @@ -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; diff --git a/packages/@aws-cdk/aws-ecs/test/cluster.test.ts b/packages/@aws-cdk/aws-ecs/test/cluster.test.ts index 52de16b353137..594a59d0380a0 100644 --- a/packages/@aws-cdk/aws-ecs/test/cluster.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/cluster.test.ts @@ -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: [], + }); + +}); \ No newline at end of file diff --git a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts index 524551aba1286..a5153b82d331a 100644 --- a/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/container-definition.test.ts @@ -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', () => { diff --git a/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts b/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts index 4f80d48118bc6..966473e5cc6cb 100644 --- a/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts +++ b/packages/@aws-cdk/aws-ecs/test/ec2/ec2-task-definition.test.ts @@ -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', () => {