Skip to content

Commit 45b68d5

Browse files
authored
feat(applicationautoscaling): add PredefinedMetric for Lambda provisioned concurrency autoscaling (#6394)
feat(applicationautoscaling): add PredefinedMetric for Lambda provisioned concurrency autoscaling (#6394 ) Closes #6369
1 parent 7485448 commit 45b68d5

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

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

+39
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,42 @@ capacity.scaleOnSchedule('AllowDownscalingAtNight', {
157157
schedule: autoscaling.Schedule.cron({ hour: '20', minute: '0' }),
158158
minCapacity: 1
159159
});
160+
```
161+
162+
## Examples
163+
164+
### Lambda Provisioned Concurrency Auto Scaling
165+
166+
```ts
167+
const handler = new lambda.Function(this, 'MyFunction', {
168+
runtime: lambda.Runtime.PYTHON_3_7,
169+
handler: 'index.handler',
170+
code: new lambda.InlineCode(`
171+
import json, time
172+
def handler(event, context):
173+
time.sleep(1)
174+
return {
175+
'statusCode': 200,
176+
'body': json.dumps('Hello CDK from Lambda!')
177+
}`),
178+
reservedConcurrentExecutions: 2,
179+
});
180+
181+
const fnVer = handler.addVersion('CDKLambdaVersion', undefined, 'demo alias', 10);
182+
183+
new apigateway.LambdaRestApi(this, 'API', { handler: fnVer })
184+
185+
const target = new applicationautoscaling.ScalableTarget(this, 'ScalableTarget', {
186+
serviceNamespace: applicationautoscaling.ServiceNamespace.LAMBDA,
187+
maxCapacity: 100,
188+
minCapacity: 10,
189+
resourceId: `function:${handler.functionName}:${fnVer.version}`,
190+
scalableDimension: 'lambda:function:ProvisionedConcurrency',
191+
})
192+
s
193+
target.scaleToTrackMetric('PceTracking', {
194+
targetValue: 0.9,
195+
predefinedMetric: applicationautoscaling.PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION,
196+
})
197+
}
198+
```

packages/@aws-cdk/aws-applicationautoscaling/lib/target-tracking-scaling-policy.ts

+49
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,64 @@ function renderCustomMetric(metric?: cloudwatch.IMetric): CfnScalingPolicy.Custo
173173
* One of the predefined autoscaling metrics
174174
*/
175175
export enum PredefinedMetric {
176+
/**
177+
* DYNAMODB_READ_CAPACITY_UTILIZATIO
178+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
179+
*/
176180
DYNAMODB_READ_CAPACITY_UTILIZATION = 'DynamoDBReadCapacityUtilization',
181+
/**
182+
* DYANMODB_WRITE_CAPACITY_UTILIZATION
183+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
184+
*/
177185
DYANMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization',
186+
/**
187+
* ALB_REQUEST_COUNT_PER_TARGET
188+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
189+
*/
178190
ALB_REQUEST_COUNT_PER_TARGET = 'ALBRequestCountPerTarget',
191+
/**
192+
* RDS_READER_AVERAGE_CPU_UTILIZATION
193+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
194+
*/
179195
RDS_READER_AVERAGE_CPU_UTILIZATION = 'RDSReaderAverageCPUUtilization',
196+
/**
197+
* RDS_READER_AVERAGE_DATABASE_CONNECTIONS
198+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
199+
*/
180200
RDS_READER_AVERAGE_DATABASE_CONNECTIONS = 'RDSReaderAverageDatabaseConnections',
201+
/**
202+
* EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION
203+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
204+
*/
181205
EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION = 'EC2SpotFleetRequestAverageCPUUtilization',
206+
/**
207+
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN
208+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
209+
*/
182210
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN = 'EC2SpotFleetRequestAverageNetworkIn',
211+
/**
212+
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT
213+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
214+
*/
183215
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT = 'EC2SpotFleetRequestAverageNetworkOut',
216+
/**
217+
* SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE
218+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
219+
*/
184220
SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE = 'SageMakerVariantInvocationsPerInstance',
221+
/**
222+
* ECS_SERVICE_AVERAGE_CPU_UTILIZATION
223+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
224+
*/
185225
ECS_SERVICE_AVERAGE_CPU_UTILIZATION = 'ECSServiceAverageCPUUtilization',
226+
/**
227+
* ECS_SERVICE_AVERAGE_CPU_UTILIZATION
228+
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
229+
*/
186230
ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION = 'ECSServiceAverageMemoryUtilization',
231+
/**
232+
* LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION
233+
* @see https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html#monitoring-metrics-concurrency
234+
*/
235+
LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION = "LambdaProvisionedConcurrencyUtilization",
187236
}

packages/@aws-cdk/aws-applicationautoscaling/test/test.target-tracking.ts

+24
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ export = {
3030
test.done();
3131
},
3232

33+
'test setup target tracking on predefined metric for lambda'(test: Test) {
34+
// GIVEN
35+
const stack = new cdk.Stack();
36+
const target = createScalableTarget(stack);
37+
38+
// WHEN
39+
target.scaleToTrackMetric('Tracking', {
40+
predefinedMetric: appscaling.PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION,
41+
targetValue: 0.9,
42+
});
43+
44+
// THEN
45+
expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', {
46+
PolicyType: "TargetTrackingScaling",
47+
TargetTrackingScalingPolicyConfiguration: {
48+
PredefinedMetricSpecification: { PredefinedMetricType: "LambdaProvisionedConcurrencyUtilization" },
49+
TargetValue: 0.9
50+
}
51+
52+
}));
53+
54+
test.done();
55+
},
56+
3357
'test setup target tracking on custom metric'(test: Test) {
3458
// GIVEN
3559
const stack = new cdk.Stack();

0 commit comments

Comments
 (0)