Skip to content

Commit

Permalink
feat(aws-autoscaling): add support for NewInstancesProtectedFromScale…
Browse files Browse the repository at this point in the history
…In (aws#14283)

Add support for `NewInstancesProtectedFromScaleIn`. This is a prerequisite for a forthcoming enhancement to improve the aws-ecs module so it can leverage EC2 Capacity Providers.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
otterley authored and john-tipper committed May 10, 2021
1 parent d8d93e2 commit 208e663
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
NewInstancesProtectedFromScaleIn: true,
}));
});

test('can use Vpc imported from unparseable list tokens', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down

0 comments on commit 208e663

Please sign in to comment.