Lambda alias as input for ApiGatewayToLambda construct

0

Background: We are using CDK to create a infrastructure. We use rest api gateway and lambda and integrate them together. We use "software.amazon.awsconstructs.services.apigatewaylambda.ApiGatewayToLambda" construct for this integration. The sample code below.

apiGatewayToLambda = ApiGatewayToLambda.Builder
            .create(this, stageId + names.componentName + "RestApi")
            .existingLambdaObj(lambdaFunction)
            .apiGatewayProps(
                mapOf(
                    "proxy" to false,
                    "restApiName" to "${waveId}${stageId}${names.componentName}RestApi",
                    "deployOptions" to deployOptions
                )
            ).build()
    }

Problem: We want to provision the lambda since the we are getting "504 - gateway timeout" from API gateway. On investigation we found that the lambda is getting timeout due to cold start.

Blocker: The lambda provisioning can be done only to lambda alias. When we analyze the ApiGatewayToLambda , it does n't accept the lambda alias as input.

How do we go about it ? Are there any workaround ? Can some one from support help me with the path forward ? Some java cdk code samples will be helpful.

1 Answer
0
Accepted Answer
Function function = (Function) lambdaStack.getAlias().getLambda()
ApiGatewayToLambda integrationSolution = ApiGatewayToLambda.Builder
            .create(this, "ApiGatewayToLambdaPattern")
            .existingLambdaObj(function)
            .build();

Kindly notice how the IFunction interface is extracted from the Alias using the getLambda() method, which I then need to cast into a Function object.

[1] - existingLambdaObj source code https://github.com/awslabs/aws-solutions-constructs/blob/main/source/patterns/%40aws-solutions-constructs/aws-apigateway-lambda/lib/index.ts#L32

[2] - Allow Lambda Aliases To Be Used With Constructs #208 https://github.com/awslabs/aws-solutions-constructs/issues/208

[3] - getLambda() https://docs.aws.amazon.com/cdk/api/v2/java/software/amazon/awscdk/services/lambda/Alias.html#getLambda

Solved this issue with the help of AWS Support, sharing here to make the solution available in this public forum, I hope it helps others.

answered 2 years ago
profile picture
EXPERT
reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions