Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(aws-autoscaling): Add support for termination policies #17936

Merged
merged 6 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 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 @@ -21,6 +21,7 @@ import { BasicLifecycleHookProps, LifecycleHook } from './lifecycle-hook';
import { BasicScheduledActionProps, ScheduledAction } from './scheduled-action';
import { BasicStepScalingPolicyProps, StepScalingPolicy } from './step-scaling-policy';
import { BaseTargetTrackingProps, PredefinedMetric, TargetTrackingScalingPolicy } from './target-tracking-scaling-policy';
import { TerminationPolicy } from './termination-policy';
import { BlockDevice, BlockDeviceVolume, EbsDeviceVolumeType } from './volume';

/**
Expand Down Expand Up @@ -314,6 +315,16 @@ export interface CommonAutoScalingGroupProps {
* @default - Auto generated by CloudFormation
*/
readonly autoScalingGroupName?: string;

/**
* A policy or a list of policies that are used to select the instances to
* terminate. The policies are executed in the order that you list them.
*
* @see https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-termination.html
*
* @default - `TerminationPolicy.DEFAULT`
*/
readonly terminationPolicies?: TerminationPolicy[];
}

/**
Expand Down Expand Up @@ -1052,6 +1063,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
healthCheckGracePeriod: props.healthCheck && props.healthCheck.gracePeriod && props.healthCheck.gracePeriod.toSeconds(),
maxInstanceLifetime: this.maxInstanceLifetime ? this.maxInstanceLifetime.toSeconds() : undefined,
newInstancesProtectedFromScaleIn: Lazy.any({ produce: () => this.newInstancesProtectedFromScaleIn }),
terminationPolicies: props.terminationPolicies,
};

if (!hasPublic && props.associatePublicIpAddress) {
Expand Down
3 changes: 2 additions & 1 deletion packages/@aws-cdk/aws-autoscaling/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export * from './scheduled-action';
export * from './step-scaling-action';
export * from './step-scaling-policy';
export * from './target-tracking-scaling-policy';
export * from './termination-policy';
export * from './volume';

// AWS::AutoScaling CloudFormation Resources:
export * from './autoscaling.generated';
export * from './autoscaling.generated';
42 changes: 42 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/termination-policy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Specifies the termination criteria to apply before Amazon EC2 Auto Scaling
* chooses an instance for termination.
*/
export enum TerminationPolicy {
/**
* Terminate instances in the Auto Scaling group to align the remaining
* instances to the allocation strategy for the type of instance that is
* terminating (either a Spot Instance or an On-Demand Instance).
*/
ALLOCATION_STRATEGY = 'AllocationStrategy',

/**
* Terminate instances that are closest to the next billing hour.
*/
CLOSEST_TO_NEXT_INSTANCE_HOUR = 'ClosestToNextInstanceHour',

/**
* Terminate instances according to the default termination policy.
*/
DEFAULT = 'Default',

/**
* Terminate the newest instance in the group.
*/
NEWEST_INSTANCE = 'NewestInstance',

/**
* Terminate the oldest instance in the group.
*/
OLDEST_INSTANCE = 'OldestInstance',

/**
* Terminate instances that have the oldest launch configuration.
*/
OLDEST_LAUNCH_CONFIGURATION = 'OldestLaunchConfiguration',

/**
* Terminate instances that have the oldest launch template.
*/
OLDEST_LAUNCH_TEMPLATE = 'OldestLaunchTemplate',
}
25 changes: 25 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 @@ -1382,6 +1382,31 @@ describe('auto scaling group', () => {
},
});
});

test('supports termination policies', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyASG', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ec2.MachineImage.latestAmazonLinux(),
terminationPolicies: [
autoscaling.TerminationPolicy.OLDEST_INSTANCE,
autoscaling.TerminationPolicy.DEFAULT,
],
});

// THEN
expect(stack).toHaveResource('AWS::AutoScaling::AutoScalingGroup', {
TerminationPolicies: [
'OldestInstance',
'Default',
],
});
});
});

function mockVpc(stack: cdk.Stack) {
Expand Down
19 changes: 1 addition & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -760,14 +760,6 @@
chalk "^4.1.2"
semver "^7.3.5"

"@jsii/check-node@1.47.0":
version "1.47.0"
resolved "https://registry.npmjs.org/@jsii/check-node/-/check-node-1.47.0.tgz#746a548b9de6b4fced4d57d6fa0384943e6a9576"
integrity sha512-LSlbKTpMVYw1R3Be70sJJdJbuLWEFAMbGEHE731Je1QDTXTRm6Gc3NDvPUvTTuHEry8f2Wys+1pXNX06X4PKxQ==
dependencies:
chalk "^4.1.2"
semver "^7.3.5"

"@jsii/spec@1.47.0", "@jsii/spec@^1.47.0":
version "1.47.0"
resolved "https://registry.npmjs.org/@jsii/spec/-/spec-1.47.0.tgz#6bc24dbf9f4949a0260d9fb51a2f9d7b30c6b4f4"
Expand Down Expand Up @@ -1878,7 +1870,7 @@
dependencies:
"@types/istanbul-lib-report" "*"

"@types/jest@^26.0.24":
"@types/jest@^26.0.22", "@types/jest@^26.0.24":
version "26.0.24"
resolved "https://registry.npmjs.org/@types/jest/-/jest-26.0.24.tgz#943d11976b16739185913a1936e0de0c4a7d595a"
integrity sha512-E/X5Vib8BWqZNRlDxj9vYXhsDwPYbPINqKF9BsnSoon4RQ0D9moEuLD8txgyypFLH7J4+Lho9Nr/c8H0Fi+17w==
Expand Down Expand Up @@ -3113,15 +3105,6 @@ codemaker@^1.47.0:
decamelize "^5.0.1"
fs-extra "^9.1.0"

codemaker@^1.47.0:
version "1.47.0"
resolved "https://registry.npmjs.org/codemaker/-/codemaker-1.47.0.tgz#f54ba966fd6e7bec9fd3394cca9cea6bcc528505"
integrity sha512-3Ab891O2IKCAOJE1rrgHS1z91AKlxoeQ2gfvL9bDv2K7zSrEN0IwI/YCgrIsUsf1RQFIOKDnizhFjn2PAap8Wg==
dependencies:
camelcase "^6.2.1"
decamelize "^5.0.1"
fs-extra "^9.1.0"

collect-v8-coverage@^1.0.0:
version "1.0.1"
resolved "https://registry.npmjs.org/collect-v8-coverage/-/collect-v8-coverage-1.0.1.tgz#cc2c8e94fc18bbdffe64d6534570c8a673b27f59"
Expand Down