Deploying a MicroProfile / Quarkus Application as AWS Lambda integrated with Application Load Balancer (ALB / ELB)

To expose a Quarkus / MicroProfile application as an AWS Lambda behind an Application Load Balancer (ALB / ELB), you need a (public) VPC:


import software.amazon.awscdk.services.ec2.Vpc;
//...
var vpc = Vpc.Builder.create(this, "VPC")
        .cidr("10.0.0.0/16")
        .enableDnsHostnames(true)
        .enableDnsSupport(true)
        .natGateways(0)
        .maxAzs(2)
    .build();

...a load balancer:


var loadBalancer = ApplicationLoadBalancer.Builder.create(this, "ELB")
                                        .internetFacing(true)
                                        .loadBalancerName(loadBalancerName)
                                        .vpc(vpc)
                                    .build();         

...and a function:


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;

//...

var function = 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();

The Lambda function is integrated with the ALB as a HTTP-listener's target:


import software.amazon.awscdk.services.elasticloadbalancingv2.AddApplicationTargetsProps;
import software.amazon.awscdk.services.elasticloadbalancingv2.BaseApplicationListenerProps;
import software.amazon.awscdk.services.elasticloadbalancingv2.HealthCheck;
import software.amazon.awscdk.services.elasticloadbalancingv2.targets.LambdaTarget;

var lambdaTarget = new LambdaTarget(function);
var listener = loadBalancer.addListener("Http", BaseApplicationListenerProps.builder()
                                                                    .port(80)
                                                                    .build()); 
                                                                    
listener.addTargets("Lambda", AddApplicationTargetsProps.builder()
                                .targets(List.of(lambdaTarget))
                                .healthCheck(HealthCheck.builder()
                                                .enabled(true)
                                                .build())
                                .build());    

The entire code ships with the AWS Quarkus Lambda CDK template.

Checkout the deployment and walk-through:

Comments:

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