adam bien's blog

AWS Lambda with Provisioned Concurrency: How to use the latest deployment with HTTP API Gateway 📎

To use the latest AWS Lambda deployment with HTTP API Gateway (or REST API Gateway):

import software.amazon.awscdk.services.apigatewayv2.alpha.HttpApi;
import software.amazon.awscdk.services.apigatewayv2.integrations.alpha.HttpLambdaIntegration;
//...
void integrateWithHTTPApiGateway(IFunction function) {
    var lambdaIntegration = HttpLambdaIntegration.Builder.create("HttpApiGatewayIntegration", function).build();
    var httpApiGateway = HttpApi.Builder.create(this, "HttpApiGatewayIntegration")
        .defaultIntegration(lambdaIntegration).build();
    var url = httpApiGateway.getUrl();
    CfnOutput.Builder.create(this, "HttpApiGatewayUrlOutput").value(url).build();
}

...configured with provisioned concurrency, you need to create an alias function.addAlias("XYZ"); (this convenience method was introduced with CDK v2.23.0) in addition to the version and use the function instance for the integration:

        
import software.amazon.awscdk.Duration;
import software.amazon.awscdk.services.lambda.Architecture;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.Runtime;
import software.amazon.awscdk.services.lambda.Version;
import software.constructs.Construct;

public class QuarkusLambda extends Construct {

    static String lambdaHandler = "io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest";
    static int memory = 1024; // ~0.5 vCPU
    static int provisionedConcurrency = 1;
    static int timeout = 10;
    Function function;

    public QuarkusLambda(Construct scope, String functionName) {
        super(scope, "QuarkusLambda");
        this.function = createFunction(functionName, lambdaHandler, memory, timeout);
        
        Version.Builder.create(this, "ProvisionedConcurrencyVersion")
                .lambda(function)
                .provisionedConcurrentExecutions(provisionedConcurrency)
                .build();
        function.addAlias("recent");
        integrateWithHTTPApiGateway(function);
        
    }

    Function createFunction(String functionName, String functionHandler, int memory, int timeout) {
        return Function.Builder.create(this, functionName)
                .runtime(Runtime.JAVA_11)
                .architecture(Architecture.ARM_64)
                .code(Code.fromAsset("../lambda/target/function.zip"))
                .handler(functionHandler)
                .memorySize(memory)
                .functionName(functionName)
                .timeout(Duration.seconds(timeout))
                .build();
    }

}

The code is based on quarkus AWS Lambda template.