adam bien's blog

Reducing AWS Lambda Cold Starts with Application Load Balancer 📎

A stock MicroProfile Health check:

import java.lang.System.Logger;
import java.lang.System.Logger.Level;

import javax.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.health.HealthCheck;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.Readiness;

@Readiness
@ApplicationScoped
public class Probe implements HealthCheck{

    static Logger LOG = System.getLogger("health");

    @Override
    public HealthCheckResponse call() {
        LOG.log(Level.INFO, "health check requested");
        return HealthCheckResponse.up("quarkus_lambda");
    }    
}    

...delivers an additional HTTP endpoint and is a good ping target for Application Load Balancer health checks:


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 listener = loadBalancer.addListener("Http", BaseApplicationListenerProps.builder()
    .port(80)
    .build()); 
    
listener.addTargets("Lambda", AddApplicationTargetsProps.builder()
    .targets(List.of(lambdaTarget))
    .healthCheck(HealthCheck.builder()
                    .path("/q/health")
                    .timeout(Duration.seconds(10))
                    .interval(Duration.seconds(15))
                    .enabled(true)
                    .build())
    .build());  

The health check /q/health is polled by the Application Load Balancer every 15 seconds, keeps the AWS Lambda warm and reduces the probability of cold starts.

The template is available from: github.com/AdamBien/aws-quarkus-lambda-cdk-plain.

AWS Lambda integration with Application Load Balancer is covered in the post: "Deploying a MicroProfile / Quarkus Application as AWS Lambda integrated with Application Load Balancer (ALB / ELB)"

See it in action: