Skip to content

Commit d2c63f5

Browse files
authored
feat(autoscaling): L2 construct for enabling capacity rebalance of autoscaling (#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*
1 parent 2902043 commit d2c63f5

12 files changed

+1932
-0
lines changed

packages/@aws-cdk/aws-autoscaling/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,26 @@ new autoscaling.AutoScalingGroup(this, 'ASG', {
518518
});
519519
```
520520

521+
## Configuring Capacity Rebalancing
522+
523+
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.
524+
525+
```ts
526+
declare const vpc: ec2.Vpc;
527+
declare const instanceType: ec2.InstanceType;
528+
declare const machineImage: ec2.IMachineImage;
529+
530+
new autoscaling.AutoScalingGroup(this, 'ASG', {
531+
vpc,
532+
instanceType,
533+
machineImage,
534+
535+
// ...
536+
537+
capacityRebalance: true,
538+
});
539+
```
540+
521541
## Configuring Instance Metadata Service (IMDS)
522542

523543
### Toggling IMDSv1

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

+13
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,18 @@ export interface CommonAutoScalingGroupProps {
353353
* @default None
354354
*/
355355
readonly defaultInstanceWarmup?: Duration;
356+
357+
/**
358+
* Indicates whether Capacity Rebalancing is enabled. When you turn on Capacity Rebalancing, Amazon EC2 Auto Scaling
359+
* attempts to launch a Spot Instance whenever Amazon EC2 notifies that a Spot Instance is at an elevated risk of
360+
* interruption. After launching a new instance, it then terminates an old instance.
361+
*
362+
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-as-group.html#cfn-as-group-capacityrebalance
363+
*
364+
* @default false
365+
*
366+
*/
367+
readonly capacityRebalance?: boolean;
356368
}
357369

358370
/**
@@ -1364,6 +1376,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
13641376
newInstancesProtectedFromScaleIn: Lazy.any({ produce: () => this.newInstancesProtectedFromScaleIn }),
13651377
terminationPolicies: props.terminationPolicies,
13661378
defaultInstanceWarmup: props.defaultInstanceWarmup?.toSeconds(),
1379+
capacityRebalance: props.capacityRebalance,
13671380
...this.getLaunchSettings(launchConfig, props.launchTemplate, props.mixedInstancesPolicy),
13681381
};
13691382

packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,27 @@ describe('auto scaling group', () => {
13451345

13461346
});
13471347

1348+
test('Can set Capacity Rebalancing via constructor property', () => {
1349+
// GIVEN
1350+
const stack = new cdk.Stack();
1351+
const vpc = mockVpc(stack);
1352+
1353+
// WHEN
1354+
new autoscaling.AutoScalingGroup(stack, 'MyASG', {
1355+
instanceType: ec2.InstanceType.of(ec2.InstanceClass.M4, ec2.InstanceSize.MICRO),
1356+
machineImage: new ec2.AmazonLinuxImage(),
1357+
vpc,
1358+
capacityRebalance: true,
1359+
});
1360+
1361+
// THEN
1362+
Template.fromStack(stack).hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
1363+
CapacityRebalance: true,
1364+
});
1365+
1366+
});
1367+
1368+
13481369
test('Can protect new instances from scale-in via constructor property', () => {
13491370
// GIVEN
13501371
const stack = new cdk.Stack();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "29.0.0",
3+
"files": {
4+
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
5+
"source": {
6+
"path": "CapacityRebalanceTestDefaultTestDeployAssert759ED6BE.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"Parameters": {
3+
"BootstrapVersion": {
4+
"Type": "AWS::SSM::Parameter::Value<String>",
5+
"Default": "/cdk-bootstrap/hnb659fds/version",
6+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
7+
}
8+
},
9+
"Rules": {
10+
"CheckBootstrapVersion": {
11+
"Assertions": [
12+
{
13+
"Assert": {
14+
"Fn::Not": [
15+
{
16+
"Fn::Contains": [
17+
[
18+
"1",
19+
"2",
20+
"3",
21+
"4",
22+
"5"
23+
],
24+
{
25+
"Ref": "BootstrapVersion"
26+
}
27+
]
28+
}
29+
]
30+
},
31+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
32+
}
33+
]
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "29.0.0",
3+
"files": {
4+
"e92ef8b6a0364fa8466b36fe291bb03b48203d6b24059b42bed94140c19551f6": {
5+
"source": {
6+
"path": "aws-cdk-autoscaling-integ.template.json",
7+
"packaging": "file"
8+
},
9+
"destinations": {
10+
"current_account-current_region": {
11+
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
12+
"objectKey": "e92ef8b6a0364fa8466b36fe291bb03b48203d6b24059b42bed94140c19551f6.json",
13+
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
14+
}
15+
}
16+
}
17+
},
18+
"dockerImages": {}
19+
}

0 commit comments

Comments
 (0)