Serverless computing is one of the best ways to run applications without managing servers. AWS Lambda lets you run your code in response to eventsβonly when neededβand Micronaut is perfect for this.
Why? Because Micronaut is lightweight and designed for fast startup times, which makes it ideal for cold-start-sensitive environments like AWS Lambda.
In this tutorial, you’ll learn how to:
- Create a Micronaut project
- Use the correct AWS Lambda runtime
- Package it using Maven
- Deploy to AWS Lambda
- Test the Lambda locally
Weβll use the package com.kscodes.micronaut.aws
throughout this post.

π§ What Youβll Need
- Java 17+
- Maven 3.6+
- AWS CLI configured
- Docker (for testing)
- Micronaut CLI (optional)
π¦ Step 1: Create the Micronaut Project
Use the CLI or Launch UI:
1 2 3 4 5 6 7 |
mn create-app com.kscodes.micronaut.aws.lambda \ --features aws-lambda,aws-lambda-handler-api-proxy \ --build maven \ --jdk 17 |
This creates a base project tailored for AWS Lambda + API Gateway.
π Directory Structure
Youβll get a project like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
. βββ src β βββ main β βββ java β βββ com β βββ kscodes β βββ micronaut β βββ aws β βββ FunctionRequestHandler.java βββ pom.xml βββ Dockerfile (for testing) |
π¨βπ» Step 2: Create a Simple Controller
File: com.kscodes.micronaut.aws.HelloController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.kscodes.micronaut.aws; import io.micronaut.http.annotation.*; @Controller("/hello") public class HelloController { @Get public String sayHello() { return "Hello from Micronaut on AWS Lambda!"; } } |
π€ Step 3: Understand the Lambda Handler
Micronaut generates this file for you:
File: com.kscodes.micronaut.aws.FunctionRequestHandler.java
1 2 3 4 5 6 7 8 |
package com.kscodes.micronaut.aws; import io.micronaut.function.aws.proxy.MicronautLambdaHandler; public class FunctionRequestHandler extends MicronautLambdaHandler {} |
This is the entry point for Lambdaβit bootstraps your Micronaut app and handles requests.
βοΈ Step 4: Maven Configuration (pom.xml
)
Ensure the following plugin is included:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<plugin> <groupId>io.micronaut.build</groupId> <artifactId>micronaut-maven-plugin</artifactId> <version>4.0.0</version> <executions> <execution> <goals> <goal>native-image</goal> </goals> </execution> </executions> </plugin> |
This helps with AOT compilation and GraalVM if you want faster cold starts.
π Step 5: Package Your Lambda
Run the following Maven command:
1 2 3 4 |
./mvnw clean package |
It will generate a zip file in:
1 2 3 4 |
target/function.zip |
This is the deployable artifact for AWS Lambda.
βοΈ Step 6: Deploy to AWS Lambda
1. Create Lambda Function
1 2 3 4 5 6 7 8 9 10 11 |
aws lambda create-function \ --function-name micronaut-lambda \ --runtime java17 \ --handler com.kscodes.micronaut.aws.FunctionRequestHandler \ --memory-size 512 \ --timeout 10 \ --zip-file fileb://target/function.zip \ --role arn:aws:iam::<account-id>:role/<lambda-execution-role> |
Make sure your IAM role has the basic Lambda permissions.
π Step 7: Add API Gateway Trigger (Optional)
To invoke via HTTP:
- Create a new REST API in API Gateway
- Create a POST method that triggers your Lambda
- Deploy the API and test with
curl
1 2 3 4 |
curl https://<api-id>.execute-api.<region>.amazonaws.com/hello |
π§ͺ Step 8: Local Testing with Docker (Optional)
Add this to Dockerfile
if you want to test locally:
1 2 3 4 5 6 |
FROM public.ecr.aws/lambda/java:latest COPY target/function.zip ${LAMBDA_TASK_ROOT} CMD [ "com.kscodes.micronaut.aws.FunctionRequestHandler::handleRequest" ] |
Then run:
1 2 3 4 5 |
docker build -t micronaut-lambda . docker run -p 9000:8080 micronaut-lambda |
Test it:
1 2 3 4 5 |
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" \ -d '{}' |
π External Resources
β Conclusion
Deploying Micronaut on AWS Lambda is a smart way to combine the speed of serverless with the power of Java. With Micronautβs AOT design and built-in AWS support, you can deploy apps with fast startup and low resource usage.
By following this guide and using Maven with the package com.kscodes.micronaut.aws
, youβll be ready to run your Micronaut services in the cloud with confidence.