Effectively running Java on ServerlessQuarkus2 min

Quarkus is a microJVM framework for Java developers that provides the capability of building Java applications with a fast boot time, and a lower time-to-first request. When combined with GraalVM native image, Quarkus will compile ahead-of-time (AOT).

The quarkus-amazon-lambda extension allows use of Quarkus to build Lambda functions. Lambda functions built with Quarkus can be deployed using the managed Java runtime, or as a native executable in Lambda custom runtime for a smaller memory footprint and faster cold boot startup time.

Benefits

  • Build time processing and reduction of reflection usage
  • Reduced startup time
  • Lower memory footprint
  • Support for GraalVM native images
  • Extensions for a variety of AWS services (KMS, S3, SQS, SNS, DynamoDB)
  • Kubernetes and AWS Lambda extensions

Implementation in AWS SAM

Using AWS SAM template, you can create a Lambda function resource as the following:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  lambda-quarkus-sample
  Sample SAM Template for lambda-quarkus

Resources:
  GetProductByIdFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: target/quarkus-lambda-function-1.0.0-SNAPSHOT-aws.jar
      Handler: io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest
      Runtime: java17
      Timeout: 30
      MemorySize: 1024
      Environment:
        Variables:
          quarkus_lambda_handler: getProductById

The corresponding Lambda function will use Jakarta's @Named to match the environment variable value:

@Named("getProductById")
public class GetProductByIdHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
  
  public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent requestEvent, Context context) { }

Quarkus AWS Lambda extension automatically enables Lambda SnapStart optimizations.

Refer the sample application for additional details.


Created by:

Dhiraj Mahapatro
Dhiraj MahapatroPrincipal Specialist SA, Serverless, AWS.
Lavanya Tangutur
Lavanya TanguturSr. Technical Account Manager