forked from aws/aws-cdk
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autoscaling): support warm pools (aws#19214)
*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* ### Summary This PR adds an interface to create a warm pool easily, which gives an ability to decrease latency for applications that have exceptionally long boot times: https://docs.aws.amazon.com/autoscaling/ec2/userguide/ec2-auto-scaling-warm-pools.html ### Test - Run unit test and integration test. ``` PASS test/warm-pool.test.js ... =============================== Coverage summary =============================== Statements : 93.22% ( 413/443 ) Branches : 87% ( 288/331 ) Functions : 93.39% ( 99/106 ) Lines : 92.95% ( 396/426 ) ================================================================================ ... Verifying integ.warm-pool.js against integ.warm-pool.expected.json ... OK. ... ``` ### Notes - I borrowed description from [AWS public doc](https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_PutWarmPool.html) for docstring.
- Loading branch information
1 parent
8978a90
commit cf965df
Showing
8 changed files
with
750 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import { Lazy, Names, Resource } from '@aws-cdk/core'; | ||
import { Construct } from 'constructs'; | ||
import { IAutoScalingGroup } from './auto-scaling-group'; | ||
import { CfnWarmPool } from './autoscaling.generated'; | ||
|
||
/** | ||
* Options for a warm pool | ||
*/ | ||
export interface WarmPoolOptions { | ||
/** | ||
* Indicates whether instances in the Auto Scaling group can be returned to the warm pool on scale in. | ||
* | ||
* If the value is not specified, instances in the Auto Scaling group will be terminated | ||
* when the group scales in. | ||
* | ||
* @default false | ||
*/ | ||
readonly reuseOnScaleIn?: boolean; | ||
|
||
/** | ||
* The maximum number of instances that are allowed to be in the warm pool | ||
* or in any state except Terminated for the Auto Scaling group. | ||
* | ||
* If the value is not specified, Amazon EC2 Auto Scaling launches and maintains | ||
* the difference between the group's maximum capacity and its desired capacity. | ||
* | ||
* @default - max size of the Auto Scaling group | ||
*/ | ||
readonly maxGroupPreparedCapacity?: number; | ||
/** | ||
* The minimum number of instances to maintain in the warm pool. | ||
* | ||
* @default 0 | ||
*/ | ||
readonly minSize?: number; | ||
/** | ||
* The instance state to transition to after the lifecycle actions are complete. | ||
* | ||
* @default PoolState.STOPPED | ||
*/ | ||
readonly poolState?: PoolState; | ||
} | ||
|
||
/** | ||
* Properties for a warm pool | ||
*/ | ||
export interface WarmPoolProps extends WarmPoolOptions { | ||
/** | ||
* The Auto Scaling group to add the warm pool to. | ||
*/ | ||
readonly autoScalingGroup: IAutoScalingGroup; | ||
} | ||
|
||
/** | ||
* Define a warm pool | ||
*/ | ||
export class WarmPool extends Resource { | ||
constructor(scope: Construct, id: string, props: WarmPoolProps) { | ||
super(scope, id, { | ||
physicalName: Lazy.string({ produce: () => Names.uniqueId(this) }), | ||
}); | ||
|
||
if (props.maxGroupPreparedCapacity && props.maxGroupPreparedCapacity < -1) { | ||
throw new Error('\'maxGroupPreparedCapacity\' parameter should be greater than or equal to -1'); | ||
} | ||
|
||
if (props.minSize && props.minSize < 0) { | ||
throw new Error('\'minSize\' parameter should be greater than or equal to 0'); | ||
} | ||
|
||
new CfnWarmPool(this, 'Resource', { | ||
autoScalingGroupName: props.autoScalingGroup.autoScalingGroupName, | ||
instanceReusePolicy: props.reuseOnScaleIn !== undefined ? { | ||
reuseOnScaleIn: props.reuseOnScaleIn, | ||
} : undefined, | ||
maxGroupPreparedCapacity: props.maxGroupPreparedCapacity, | ||
minSize: props.minSize, | ||
poolState: props.poolState, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* The instance state in the warm pool | ||
*/ | ||
export enum PoolState { | ||
/** | ||
* Hibernated | ||
* | ||
* To use this state, prerequisites must be in place. | ||
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/hibernating-prerequisites.html | ||
*/ | ||
HIBERNATED = 'Hibernated', | ||
|
||
/** | ||
* Running | ||
*/ | ||
RUNNING = 'Running', | ||
|
||
/** | ||
* Stopped | ||
*/ | ||
STOPPED = 'Stopped', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.