Deploying Micronaut on AWS Lambda: Serverless Java Simplified

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.

Deploying Micronaut on AWS Lambda

πŸ”§ 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:


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:


.
β”œβ”€β”€ 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


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


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:



  io.micronaut.build
  micronaut-maven-plugin
  4.0.0
  
    
      
        native-image
      
    
  


This helps with AOT compilation and GraalVM if you want faster cold starts.

πŸ— Step 5: Package Your Lambda

Run the following Maven command:


./mvnw clean package

It will generate a zip file in:


target/function.zip

This is the deployable artifact for AWS Lambda.

☁️ Step 6: Deploy to AWS Lambda

1. Create Lambda Function


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:::role/

Make sure your IAM role has the basic Lambda permissions.

πŸ”Œ Step 7: Add API Gateway Trigger (Optional)

To invoke via HTTP:

  1. Create a new REST API in API Gateway
  2. Create a POST method that triggers your Lambda
  3. Deploy the API and test with curl

curl https://.execute-api..amazonaws.com/hello

πŸ§ͺ Step 8: Local Testing with Docker (Optional)

Add this to Dockerfile if you want to test locally:


FROM public.ecr.aws/lambda/java:latest
COPY target/function.zip ${LAMBDA_TASK_ROOT}
CMD [ "com.kscodes.micronaut.aws.FunctionRequestHandler::handleRequest" ]

Then run:


docker build -t micronaut-lambda .
docker run -p 9000:8080 micronaut-lambda

Test it:


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.