forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
integ.autoscaling.lit.ts
58 lines (45 loc) · 1.74 KB
/
integ.autoscaling.lit.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import * as appscaling from '@aws-cdk/aws-applicationautoscaling';
import * as cdk from '@aws-cdk/core';
import { LAMBDA_RECOGNIZE_LAYER_VERSION } from '@aws-cdk/cx-api';
import * as lambda from '../lib';
/**
* Stack verification steps:
* aws application-autoscaling describe-scalable-targets --service-namespace lambda --resource-ids function:<function name>:prod
* has a minCapacity of 3 and maxCapacity of 50
*/
class TestStack extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
const fn = new lambda.Function(this, 'MyLambda', {
code: new lambda.InlineCode('exports.handler = async () => { console.log(\'hello world\'); };'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_14_X,
});
const version = fn.currentVersion;
const alias = new lambda.Alias(this, 'Alias', {
aliasName: 'prod',
version,
});
const scalingTarget = alias.addAutoScaling({ minCapacity: 3, maxCapacity: 50 });
scalingTarget.scaleOnUtilization({
utilizationTarget: 0.5,
});
scalingTarget.scaleOnSchedule('ScaleUpInTheMorning', {
schedule: appscaling.Schedule.cron({ hour: '8', minute: '0' }),
minCapacity: 20,
});
scalingTarget.scaleOnSchedule('ScaleDownAtNight', {
schedule: appscaling.Schedule.cron({ hour: '20', minute: '0' }),
maxCapacity: 20,
});
new cdk.CfnOutput(this, 'FunctionName', {
value: fn.functionName,
});
}
}
const app = new cdk.App();
const stack = new TestStack(app, 'aws-lambda-autoscaling');
// Changes the function description when the feature flag is present
// to validate the changed function hash.
cdk.Aspects.of(stack).add(new lambda.FunctionVersionUpgrade(LAMBDA_RECOGNIZE_LAYER_VERSION));
app.synth();