Adding Provisioned Concurrency for imported Lambda via cdk

Viewed 53

Problem

I have Lambda function for which i want to add provisioned concurrency.I can access the Lambda only as a lower level CfnFunction resource or a FunctionProxy imported via ARN. I do not have access to the higher level Function construct in the cdk stack directly which would give me access to the needed attributes and methods to add prov. concurrency as usual.

Details

My cdk stack setup consists of

  • an APIGateway REST API running a Lambda function as request handler
  • the Lambda request handler has no Alias or Version (uses default $LATEST)
  • the APIGateway REST API and the Lambda function are created via cdk-chalice and not defined in my stack directly.
  • this means they come from a pre-generated Cloudfomation template that gets then included into my main cdk stack template.
  • so i have no higher level construct access to the APIGateway or Lambda function
  • I only have lower level construct access (CfnFunction) from importing the Chalice generated template back in the cdk stack or imported the Lambda function via lambda.from_function_arn()

Question

  • Is there a way to add provisioned concurrency
    • via manipulating the pre-generated Cloudformation template from cdk-chalice?
    • by importing the lambda through lambda.from_function_arn() then adding a Version and ScalableTarget?
    • via some tricks adding a Version from the lower level construct CfnFunction?
    • or maybe via some AwsSdkCalls to generate a Version for the lambda and add PC wrapped in a AwsCustomResource?

What I have tried so far

Importing the Lambda

# Pseudocode
handler = lambda.from_function_arn("arn-from-cdk-chalice-function")

# approach a - cdk deploy error: No current_version attribute on BaseFunctionImportProxy_
handler_version = handler.current_version

# approach b - cdk deploy error: Application Autoscaling cannot work with $LATEST
handler_version = handler.latest_version
target = appscaling.ScalableTarget(self, "ScalableTarget",
    service_namespace=appscaling.ServiceNamespace.LAMBDA,
    max_capacity=100,
    min_capacity=10,
    resource_id=f"function:{handler.functionName}:{handler_version.version}",
    scalable_dimension="lambda:function:ProvisionedConcurrency"
)
0 Answers
Related