Skip to content

Instantly share code, notes, and snippets.

@iRoachie
Created April 20, 2023 16:00
Show Gist options
  • Save iRoachie/b8557e62c764eddccc9da687b6d5a19a to your computer and use it in GitHub Desktop.
Save iRoachie/b8557e62c764eddccc9da687b6d5a19a to your computer and use it in GitHub Desktop.
Using Provisioned Concurrency with Lambda version
import * as cdk from 'aws-cdk-lib';
import { PredefinedMetric, ScalableTarget, ServiceNamespace, TargetTrackingScalingPolicy } from 'aws-cdk-lib/aws-applicationautoscaling';
import { Role } from 'aws-cdk-lib/aws-iam';
import { Runtime, Function, Code, Version } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
import { join } from 'path';
export class TestStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const helloLambda = new Function(this, 'helloLambda', {
runtime: Runtime.NODEJS_14_X,
code: Code.fromAsset(join(__dirname, 'boom')),
handler: 'index.handler',
});
const helloLambdaV1 = new Version(this, `helloLambda-v1`, {
lambda: helloLambda,
});
const role = Role.fromRoleArn(this, 'ScalingRole', this.formatArn({
service: 'iam',
region: '',
resource: 'role/aws-service-role/lambda.application-autoscaling.amazonaws.com',
resourceName: 'AWSServiceRoleForApplicationAutoScaling_LambdaConcurrency',
}));
const target = new ScalableTarget(this, 'target', {
minCapacity: 1,
maxCapacity: 10,
resourceId: `function:${helloLambdaV1.functionName}`,
scalableDimension: 'lambda:function:ProvisionedConcurrency',
serviceNamespace: ServiceNamespace.LAMBDA,
role,
})
new TargetTrackingScalingPolicy(this, 'policy', {
scalingTarget: target,
policyName: 'policy',
targetValue: 0.7,
predefinedMetric: PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment