This project demonstrates how to deploy a Quarkus application as an AWS Lambda function using AWS CDK with TypeScript.
- Project Overview
- Prerequisites
- Getting Started
- Building the Quarkus Application
- Deploying with AWS CDK
- Deploying with AWS CLI
- Testing the Lambda Function
- LocalStack Integration
- Configuration Options
This project uses AWS CDK with TypeScript to define and provision AWS infrastructure for deploying a Quarkus application as a Lambda function. The Quarkus application can be deployed in different modes:
- JVM Mode: Traditional Java deployment
- Native Mode: GraalVM native image for faster startup and lower memory usage
- Native ARM64 Mode: Native image optimized for ARM64 architecture
The cdk.json
file tells the CDK Toolkit how to execute your app.
- Node.js and npm
- AWS CLI configured with appropriate credentials
- Maven
- Java 17 or later
- GraalVM (for native builds)
- Docker (for native builds with container)
- AWS SAM CLI (for local testing)
- QEMU (for multi-architecture builds)
To build and run containers for different CPU architectures (like x86_64 and ARM64) on a single host, you need to enable QEMU in Docker. This is essential for cross-platform development and testing, especially when building native ARM64 images on x86_64 machines.
# Install QEMU packages
sudo apt-get update
sudo apt-get install -y qemu-user-static binfmt-support
# Register QEMU in the build agent
docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
# QEMU is included with Docker Desktop for Mac
# Just make sure you have the latest version of Docker Desktop installed
# Verify QEMU is working
docker run --rm --platform=linux/arm64 arm64v8/ubuntu uname -m
# Should output: aarch64
# QEMU is included with Docker Desktop for Windows
# Make sure you have the latest version of Docker Desktop installed with WSL2 backend
# Verify QEMU is working
docker run --rm --platform=linux/arm64 arm64v8/ubuntu uname -m
# Should output: aarch64
To verify that QEMU is properly set up, run:
# Check if you can run ARM64 containers on x86_64 host (or vice versa)
docker run --rm --platform=linux/arm64 arm64v8/alpine uname -m
# Should output: aarch64
docker run --rm --platform=linux/amd64 amd64/alpine uname -m
# Should output: x86_64
npm run build
- Compile TypeScript to JavaScriptnpm run watch
- Watch for changes and compilenpm run test
- Perform the Jest unit testsnpx cdk deploy
- Deploy this stack to your default AWS account/regionnpx cdk diff
- Compare deployed stack with current statenpx cdk synth
- Emits the synthesized CloudFormation template
cdk2 deploy --profile yourProfileName
mvn package
mvn package -Pnative
mvn clean package -Pnative -Dquarkus.native.container-runtime=docker -DskipTests
Note: After building, the
function.zip
file from the target directory should be moved to a folder namedzipped
for deployment.
The project includes CDK code to deploy the Quarkus Lambda function. The deployment process is handled by the CDK stack defined in the lib
directory.
To deploy using CDK:
npm run build
npx cdk deploy
You can deploy your Quarkus Lambda function directly using the AWS CLI. This section outlines the steps for different deployment options.
-
Build the JVM package:
mvn package
-
Create the Lambda function:
aws lambda create-function \ --function-name quarkus-lambda-jvm \ --zip-file fileb://lambda-pom/quarkus-lambda/target/function.zip \ --handler io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest \ --runtime java17 \ --role arn:aws:iam::your-account-id:role/lambda-role \ --memory-size 256 \ --timeout 15 \ --environment Variables={QUARKUS_LAMBDA_HANDLER=test}
-
Build the native package:
mvn package -Pnative
-
Create the Lambda function:
aws lambda create-function \ --function-name quarkus-lambda-native \ --zip-file fileb://lambda-pom/quarkus-lambda/target/function.zip \ --handler not.used.in.provided.runtime \ --runtime provided.al2023 \ --role arn:aws:iam::your-account-id:role/lambda-role \ --memory-size 128 \ --timeout 15 \ --environment Variables={DISABLE_SIGNAL_HANDLERS=true,QUARKUS_LAMBDA_HANDLER=two}
Important: Make sure you have enabled QEMU for Docker multi-architecture support as described in the Enabling QEMU for Docker Multi-Architecture Support section before proceeding with ARM64 builds.
-
Build the native ARM64 package:
mvn clean package -Pnative -Dquarkus.native.container-runtime=docker -DskipTests
-
Create the Lambda function:
aws lambda create-function \ --function-name quarkus-lambda-native-arm \ --zip-file fileb://lambda-pom/quarkus-lambda/target/function.zip \ --handler not.used.in.provided.runtime \ --runtime provided.al2023 \ --architectures arm64 \ --role arn:aws:iam::your-account-id:role/lambda-role \ --memory-size 128 \ --timeout 15 \ --environment Variables={DISABLE_SIGNAL_HANDLERS=true}
aws lambda update-function-code \
--function-name quarkus-lambda-native \
--zip-file fileb://lambda-pom/quarkus-lambda/target/function.zip
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.jvm.yaml -e lambda-pom/quarkus-lambda/payload.json
You can pass environment variables to your Lambda function when testing locally with SAM using the --env-vars
option:
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.jvm.yaml -e lambda-pom/quarkus-lambda/payload.json --env-vars lambda-pom/quarkus-lambda/env.json
The env.json file should have the following format:
{
"FunctionName": {
"ENVIRONMENT_VARIABLE_NAME": "value"
}
}
For example, to set the QUARKUS_LAMBDA_HANDLER
to use the "test" handler:
{
"QuarkusLambda": {
"QUARKUS_LAMBDA_HANDLER": "test"
}
}
You can also specify environment variables directly on the command line:
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.jvm.yaml -e lambda-pom/quarkus-lambda/payload.json --parameter-overrides ParameterKey=QUARKUS_LAMBDA_HANDLER,ParameterValue=test
aws lambda invoke outputjson --function-name quarkus-lambda-native --payload fileb://payload.json --profile yourProfileName
Expected output:
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
This project includes SAM (Serverless Application Model) templates for different deployment scenarios. These templates define the Lambda function configuration and are used for both local testing and deployment.
This template is used for deploying the Quarkus application in JVM mode, as shown earlier in the testing section.
This template is for deploying Quarkus native executables on Amazon Linux 2023 (x86_64 architecture):
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Quarkus Native (AL2023) - quarkus-amazon-lambda
Globals:
Api:
EndpointConfiguration: REGIONAL
BinaryMediaTypes:
- "*/*"
Resources:
QuarkusLambda:
Type: AWS::Serverless::Function
Properties:
Handler: not.used.in.provided.runtime
Runtime: provided.al2023
CodeUri: function.zip
MemorySize: 128
Timeout: 15
Policies: AWSLambdaBasicExecutionRole
Environment:
Variables:
DISABLE_SIGNAL_HANDLERS: true
QUARKUS_LAMBDA_HANDLER: test
This template is specialized for deploying Quarkus native executables on Amazon Linux 2023 with ARM64 architecture, which can provide better performance and cost efficiency on AWS Graviton processors:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Quarkus Native ARM64 (AL2023) - quarkus-amazon-lambda
Globals:
Api:
EndpointConfiguration: REGIONAL
BinaryMediaTypes:
- "*/*"
Resources:
QuarkusLambda:
Type: AWS::Serverless::Function
Properties:
Handler: not.used.in.provided.runtime
Runtime: provided.al2023
Architectures:
- arm64
CodeUri: function.zip
MemorySize: 128
Timeout: 15
Policies: AWSLambdaBasicExecutionRole
Environment:
Variables:
DISABLE_SIGNAL_HANDLERS: true
QUARKUS_LAMBDA_HANDLER: test
You can use these templates for local testing with the SAM CLI:
# Test with JVM mode template
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.jvm.yaml -e lambda-pom/quarkus-lambda/payload.json
# Test with native mode template
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.native.al2023.yaml -e lambda-pom/quarkus-lambda/payload.json
# Test with native ARM64 mode template
sam local invoke -t lambda-pom/quarkus-lambda/target/sam.native.al2023-arm.yaml -e lambda-pom/quarkus-lambda/payload.json
For local development and testing, you can use LocalStack to emulate AWS services.
localstack start -d --network ls
docker inspect localstack-main | jq -r '.[0].NetworkSettings.Networks | to_entries | .[].value.IPAddress'
samlocal local invoke -t target/sam.jvm.yaml -e payload.json --docker-network ls --add-host localhost.localstack.cloud:172.25.0.2
You can also pass environment variables when using LocalStack:
samlocal local invoke -t target/sam.jvm.yaml -e payload.json --docker-network ls --add-host localhost.localstack.cloud:172.25.0.2 --env-vars lambda-pom/quarkus-lambda/env.json
Or directly on the command line:
samlocal local invoke -t target/sam.jvm.yaml -e payload.json --docker-network ls --add-host localhost.localstack.cloud:172.25.0.2 --parameter-overrides ParameterKey=QUARKUS_LAMBDA_HANDLER,ParameterValue=test
You can specify the Lambda handler using an environment variable:
QUARKUS_LAMBDA_HANDLER=s3 mvn install -DskipTests
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS Serverless Quarkus - quarkus-amazon-lambda-common-deployment
Globals:
Api:
EndpointConfiguration: REGIONAL
BinaryMediaTypes:
- "*/*"
Resources:
QuarkusLambda:
Type: AWS::Serverless::Function
Properties:
Handler: io.quarkus.amazon.lambda.runtime.QuarkusStreamHandler::handleRequest
Runtime: java17
CodeUri: function.zip
MemorySize: 256
Timeout: 15
Policies: AWSLambdaBasicExecutionRole
Environment:
Variables:
QUARKUS_LAMBDA_HANDLER: test
{
"QuarkusLambda": {
"QUARKUS_LAMBDA_HANDLER": "test"
}
}
quarkus.lambda.handler=${QUARKUS_LAMBDA_HANDLER:s3}
quarkus.ssl.native=true
quarkus.native.additional-build-args=--initialize-at-run-time=org.apache.http.impl.auth.NTLMEngineImpl
quarkus.native.builder-image=quay.io/quarkus/ubi-quarkus-mandrel-builder-image:23.1.6.0-Final-java21-arm64
This project is licensed under the MIT License - see the LICENSE file for details.