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(applicationautoscaling): add PredefinedMetric for Lambda provisioned concurrency autoscaling #6394

Merged
merged 7 commits into from
Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions packages/@aws-cdk/aws-applicationautoscaling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,42 @@ capacity.scaleOnSchedule('AllowDownscalingAtNight', {
schedule: autoscaling.Schedule.cron({ hour: '20', minute: '0' }),
minCapacity: 1
});
```

## Examples

### Lambda Provisioned Concurrency Auto Scaling

```ts
const handler = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.PYTHON_3_7,
handler: 'index.handler',
code: new lambda.InlineCode(`
import json, time
def handler(event, context):
time.sleep(1)
return {
'statusCode': 200,
'body': json.dumps('Hello CDK from Lambda!')
}`),
reservedConcurrentExecutions: 2,
});

const fnVer = handler.addVersion('CDKLambdaVersion', undefined, 'demo alias', 10);

new apigateway.LambdaRestApi(this, 'API', { handler: fnVer })

const target = new applicationautoscaling.ScalableTarget(this, 'ScalableTarget', {
serviceNamespace: applicationautoscaling.ServiceNamespace.LAMBDA,
maxCapacity: 100,
minCapacity: 10,
resourceId: `function:${handler.functionName}:${fnVer.version}`,
scalableDimension: 'lambda:function:ProvisionedConcurrency',
})
s
target.scaleToTrackMetric('PceTracking', {
targetValue: 0.9,
predefinedMetric: applicationautoscaling.PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION,
})
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,64 @@ function renderCustomMetric(metric?: cloudwatch.IMetric): CfnScalingPolicy.Custo
* One of the predefined autoscaling metrics
*/
export enum PredefinedMetric {
/**
* DYNAMODB_READ_CAPACITY_UTILIZATIO
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
DYNAMODB_READ_CAPACITY_UTILIZATION = 'DynamoDBReadCapacityUtilization',
/**
* DYANMODB_WRITE_CAPACITY_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
DYANMODB_WRITE_CAPACITY_UTILIZATION = 'DynamoDBWriteCapacityUtilization',
/**
* ALB_REQUEST_COUNT_PER_TARGET
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ALB_REQUEST_COUNT_PER_TARGET = 'ALBRequestCountPerTarget',
/**
* RDS_READER_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
RDS_READER_AVERAGE_CPU_UTILIZATION = 'RDSReaderAverageCPUUtilization',
/**
* RDS_READER_AVERAGE_DATABASE_CONNECTIONS
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
RDS_READER_AVERAGE_DATABASE_CONNECTIONS = 'RDSReaderAverageDatabaseConnections',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_CPU_UTILIZATION = 'EC2SpotFleetRequestAverageCPUUtilization',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_IN = 'EC2SpotFleetRequestAverageNetworkIn',
/**
* EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
EC2_SPOT_FLEET_REQUEST_AVERAGE_NETWORK_OUT = 'EC2SpotFleetRequestAverageNetworkOut',
/**
* SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
SAGEMAKER_VARIANT_INVOCATIONS_PER_INSTANCE = 'SageMakerVariantInvocationsPerInstance',
/**
* ECS_SERVICE_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ECS_SERVICE_AVERAGE_CPU_UTILIZATION = 'ECSServiceAverageCPUUtilization',
/**
* ECS_SERVICE_AVERAGE_CPU_UTILIZATION
* @see https://docs.aws.amazon.com/autoscaling/application/APIReference/API_PredefinedMetricSpecification.html
*/
ECS_SERVICE_AVERAGE_MEMORY_UTILIZATION = 'ECSServiceAverageMemoryUtilization',
/**
* LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION
* @see https://docs.aws.amazon.com/lambda/latest/dg/monitoring-metrics.html#monitoring-metrics-concurrency
*/
pahud marked this conversation as resolved.
Show resolved Hide resolved
LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION = "LambdaProvisionedConcurrencyUtilization",
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,30 @@ export = {
test.done();
},

'test setup target tracking on predefined metric for lambda'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const target = createScalableTarget(stack);

// WHEN
target.scaleToTrackMetric('Tracking', {
predefinedMetric: appscaling.PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION,
targetValue: 0.9,
});

// THEN
expect(stack).to(haveResource('AWS::ApplicationAutoScaling::ScalingPolicy', {
PolicyType: "TargetTrackingScaling",
TargetTrackingScalingPolicyConfiguration: {
PredefinedMetricSpecification: { PredefinedMetricType: "LambdaProvisionedConcurrencyUtilization" },
TargetValue: 0.9
}

}));

test.done();
},

'test setup target tracking on custom metric'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
Expand Down