From 9a98ae6831caa948549df6587604b6dc66bbe019 Mon Sep 17 00:00:00 2001 From: Michael Fischer Date: Tue, 20 Apr 2021 15:07:23 -0700 Subject: [PATCH] feat(aws-autoscaling): add support for NewInstancesProtectedFromScaleIn --- packages/@aws-cdk/aws-autoscaling/README.md | 17 +++++++++++++++++ .../aws-autoscaling/lib/auto-scaling-group.ts | 18 ++++++++++++++++++ .../test/auto-scaling-group.test.ts | 19 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/packages/@aws-cdk/aws-autoscaling/README.md b/packages/@aws-cdk/aws-autoscaling/README.md index 95e1a25963afe..b0711c78062c4 100644 --- a/packages/@aws-cdk/aws-autoscaling/README.md +++ b/packages/@aws-cdk/aws-autoscaling/README.md @@ -360,6 +360,23 @@ new autoscaling.AutoScalingGroup(stack, 'ASG', { }); ``` +## Protecting new instances from being terminated on scale-in + +By default, Auto Scaling can terminate an instance at any time after launch when +scaling in an Auto Scaling Group, subject to the group's [termination +policy](https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html). + +However, you may wish to protect newly-launched instances from being scaled in +if they are going to run critical applications that should not be prematurely +terminated. This protection can be removed after launch. + +```ts +new autoscaling.AutoScalingGroup(stack, 'ASG', { + newInstancesProtectedFromScaleIn: true, + // ... +}); +``` + ## Future work * [ ] CloudWatch Events (impossible to add currently as the AutoScalingGroup ARN is diff --git a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts index 42ea38ab80d11..c72a0d6a760b0 100644 --- a/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts +++ b/packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts @@ -290,6 +290,23 @@ export interface CommonAutoScalingGroupProps { */ readonly updatePolicy?: UpdatePolicy; + /** + * Whether newly-launched instances are protected from termination by Amazon + * EC2 Auto Scaling when scaling in. + * + * By default, Auto Scaling can terminate an instance at any time after launch + * when scaling in an Auto Scaling Group, subject to the group's termination + * policy. However, you may wish to protect newly-launched instances from + * being scaled in if they are going to run critical applications that should + * not beß prematurely terminated. + * + * This flag must be enabled if the Auto Scaling Group will be associated with + * an ECS Capacity Provider with managed termination protection. + * + * @default false + */ + readonly newInstancesProtectedFromScaleIn?: boolean; + /** * The name of the Auto Scaling group. This name must be unique per Region per account. * @default - Auto generated by CloudFormation @@ -1021,6 +1038,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements healthCheckType: props.healthCheck && props.healthCheck.type, healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(), maxInstanceLifetime: this.maxInstanceLifetime ? this.maxInstanceLifetime.toSeconds() : undefined, + newInstancesProtectedFromScaleIn: props.newInstancesProtectedFromScaleIn, }; if (!hasPublic && props.associatePublicIpAddress) { diff --git a/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts b/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts index 55e69dbb46eda..670b44b03212c 100644 --- a/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts +++ b/packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts @@ -1351,6 +1351,25 @@ test('Can set autoScalingGroupName', () => { })); }); +test('Can protect new instances from scale-in', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = mockVpc(stack); + + // WHEN + new autoscaling.AutoScalingGroup(stack, 'MyASG', { + instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO), + machineImage: new ec2.AmazonLinuxImage(), + vpc, + newInstancesProtectedFromScaleIn: true, + }); + + // THEN + expect(stack).to(haveResourceLike('AWS::AutoScaling::AutoScalingGroup', { + NewResourcesProtectedFromScaleIn: true, + })); +}); + test('can use Vpc imported from unparseable list tokens', () => { // GIVEN const stack = new cdk.Stack();