AWS Lambda: Exposing Java (Corretto) / MicroProfile HTTP / REST Service via Function URL without Gateways or Application Load Balancers with CDK

With Lambda function URLs you can directly expose an AWS Lambda, without REST / HTTP API Gateway or Application Load Balancer.

An AWS Lambda function:


import software.amazon.awscdk.services.lambda.IFunction;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.Runtime;
import software.constructs.Construct;

public class QuarkusLambda extends Construct{

    static Map<String, String> configuration = Map.of("message", "hello, quarkus as AWS Lambda");
    static String lambdaHandler = "io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest";
    static int memory = 1024; //~0.5 vCPU
    static int maxConcurrency = 2;
    static int timeout = 10;
    IFunction function;

    public QuarkusLambda(Construct scope,String functionName) {
        super(scope, "QuarkusLambda");
        this.function = createFunction(functionName, lambdaHandler, configuration, memory, maxConcurrency, timeout);
    }

    IFunction createFunction(String functionName,String functionHandler, Map<String,String> configuration, int memory, int maximumConcurrentExecution, 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)
                .environment(configuration)
                .timeout(Duration.seconds(timeout))
                .reservedConcurrentExecutions(maximumConcurrentExecution)
                .build();
    }    

    public IFunction getFunction(){
        return this.function;
    }
}

Can be directly exposed via a generated URL with:


var quarkuLambda = new QuarkusLambda(this, "airhacks_lambda_gretings_boundary_FUrl");
var function = quarkuLambda.getFunction();
var functionUrl = function
        .addFunctionUrl(FunctionUrlOptions.builder()
        .authType(FunctionUrlAuthType.NONE)
        .build());
CfnOutput.Builder.create(this, "FunctionURLOutput").value(functionUrl.getUrl()).build();

In this 6 minute screencast I refactored a Lambda from HTTP API Gateway to use Function URL directly:

The example in the screencast is based on the Quarkus / Lambda template: github.com/AdamBien/aws-quarkus-lambda-cdk-plain

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License