Skip to content

Commit

Permalink
feat(autoscaling): L2 construct for enabling capacity rebalance of au…
Browse files Browse the repository at this point in the history
…toscaling (#24025)

Add L2 construct for enabling capacity rebalance of autoscaling

Closes #22625.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
zorrofox committed Feb 10, 2023
1 parent 2902043 commit d2c63f5
Show file tree
Hide file tree
Showing 12 changed files with 1,932 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/README.md
Expand Up @@ -518,6 +518,26 @@ new autoscaling.AutoScalingGroup(this, 'ASG', {
});
```

## Configuring Capacity Rebalancing

Indicates whether Capacity Rebalancing is enabled. Otherwise, Capacity Rebalancing is disabled. When you turn on Capacity Rebalancing, Amazon EC2 Auto Scaling attempts to launch a Spot Instance whenever Amazon EC2 notifies that a Spot Instance is at an elevated risk of interruption. After launching a new instance, it then terminates an old instance. For more information, see [Use Capacity Rebalancing to handle Amazon EC2 Spot Interruptions](https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-capacity-rebalancing.html) in the in the Amazon EC2 Auto Scaling User Guide.

```ts
declare const vpc: ec2.Vpc;
declare const instanceType: ec2.InstanceType;
declare const machineImage: ec2.IMachineImage;

new autoscaling.AutoScalingGroup(this, 'ASG', {
vpc,
instanceType,
machineImage,

// ...

capacityRebalance: true,
});
```

## Configuring Instance Metadata Service (IMDS)

### Toggling IMDSv1
Expand Down
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Expand Up @@ -353,6 +353,18 @@ export interface CommonAutoScalingGroupProps {
* @default None
*/
readonly defaultInstanceWarmup?: Duration;

/**
* Indicates whether Capacity Rebalancing is enabled. When you turn on Capacity Rebalancing, Amazon EC2 Auto Scaling
* attempts to launch a Spot Instance whenever Amazon EC2 notifies that a Spot Instance is at an elevated risk of
* interruption. After launching a new instance, it then terminates an old instance.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-capacityrebalance
*
* @default false
*
*/
readonly capacityRebalance?: boolean;
}

/**
Expand Down Expand Up @@ -1364,6 +1376,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
newInstancesProtectedFromScaleIn: Lazy.any({ produce: () => this.newInstancesProtectedFromScaleIn }),
terminationPolicies: props.terminationPolicies,
defaultInstanceWarmup: props.defaultInstanceWarmup?.toSeconds(),
capacityRebalance: props.capacityRebalance,
...this.getLaunchSettings(launchConfig, props.launchTemplate, props.mixedInstancesPolicy),
};

Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts
Expand Up @@ -1345,6 +1345,27 @@ describe('auto scaling group', () => {

});

test('Can set Capacity Rebalancing via constructor property', () => {
// 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,
capacityRebalance: true,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
CapacityRebalance: true,
});

});


test('Can protect new instances from scale-in via constructor property', () => {
// GIVEN
const stack = new cdk.Stack();
Expand Down
@@ -0,0 +1,19 @@
{
"version": "29.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
"path": "CapacityRebalanceTestDefaultTestDeployAssert759ED6BE.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
@@ -0,0 +1,36 @@
{
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
@@ -0,0 +1,19 @@
{
"version": "29.0.0",
"files": {
"e92ef8b6a0364fa8466b36fe291bb03b48203d6b24059b42bed94140c19551f6": {
"source": {
"path": "aws-cdk-autoscaling-integ.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "e92ef8b6a0364fa8466b36fe291bb03b48203d6b24059b42bed94140c19551f6.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}

0 comments on commit d2c63f5

Please sign in to comment.