aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Setting up Lambda provisioned concurrency auto scaling against a label fails to deploy

Open ErkoKnoll opened this issue 6 years ago • 4 comments

Following CDK code that tries to set up a stack from scratch is failing when Lambda provisioned concurrency auto scaling is tracking a label rather than a version:

const lambda = new Function(...)

lambda.currentVersion.addAlias('PROVISIONED', {
    provisionedConcurrentExecutions: 1
});

const target = new ScalableTarget(this, 'ScalableTarget', {
    serviceNamespace: ServiceNamespace.LAMBDA,
    maxCapacity: 5,
    minCapacity: 1,
    resourceId: `function:${lambda.functionName}:PROVISIONED`,
    scalableDimension: 'lambda:function:ProvisionedConcurrency'
});

target.scaleToTrackMetric('PcuTracking', {
    targetValue: 0.8,
    predefinedMetric: PredefinedMetric.LAMBDA_PROVISIONED_CONCURRENCY_UTILIZATION
});

This setup throws the following error while trying to spool up a new stack: Validation failed for resource: [REMOVED], scalable dimension: lambda:function:ProvisionedConcurrency. Reason: Cannot find alias arn: arn:aws:lambda:eu-west-2:[REMOVED]:PROVISIONED (Service: AWSApplicationAutoScaling; Status Code: 400; Error Code: ValidationException; Request ID: 407f1729-2267-4339-9865-9d7c20d50224)

Looks like by the time auto scale tracking performs validation Lambda label is not set up properly and hence the failure. If I did set it up against a version then it would work fine. I also did manage to get it passed if the label already existed, but obviously when function is created from scratch then there aren't any labels.

Setting up provisioned concurrency and auto scaling against a label have couple of advantages over version:

  • Service that calls this Lambda does not have to know the latest version, only a label.
  • If this service is re-deployed then previous auto scaling concurrency count would remain the same agains a label, but would get reset to default setting against a version.

If this is not a bug then what would be the proper way to set up CDK Lambda provisioned concurrency with auto scaling that tracks a label and not a version?

CDK version: 1.37.0

ErkoKnoll avatar May 11 '20 12:05 ErkoKnoll

I poked around at this. It seems there is no good way to "force" this dependency to happen via CDK. The best you can do is add the dependency manually like so:

https://github.com/iph/lambda-experiments/blob/master/pc-asg/lib/pc-asg-stack.ts#L33

Hopefully that helps in the interim :)

iph avatar May 17 '20 04:05 iph

Thank you @iph!

The aws-applicationautoscaling module was designed to be used by other constructs libraries to offer AutoScaling features for their own constructs. This is the reason its API is very generic, the ideal implementation would be for the aws-lambda module to use the aws-applicationautoscaling to define its own autoScaling capabilities, taking care of all the necessary wiring such as adding the required dependencies. An example of such implementation is dynamodb read capacity autoscaling which is implemented using the aws-applicationautoscaling module. I will go ahed an open a feature request to implement AutoScaling in our aws-lambda module.

NetaNir avatar May 18 '20 06:05 NetaNir

Thank you @iph , your workaround works nicely!

ErkoKnoll avatar May 18 '20 13:05 ErkoKnoll

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.

github-actions[bot] avatar Dec 07 '20 02:12 github-actions[bot]