Skip to content

Commit

Permalink
feat(applicationautoscaling): add PredefinedMetric for Lambda provisi…
Browse files Browse the repository at this point in the history
…oned concurrency autoscaling (#6394)

feat(applicationautoscaling): add PredefinedMetric for Lambda provisioned concurrency autoscaling (#6394  )

Closes #6369
  • Loading branch information
pahud authored Mar 28, 2020
1 parent 7485448 commit 45b68d5
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
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
*/
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

0 comments on commit 45b68d5

Please sign in to comment.