aws-lambda-java-integration
giuseppe-trisciuoglio/developer-kit
AWS Lambda integration patterns for Java with cold start optimization below 1 second.
What is aws-lambda-java-integration?
Provides production-ready patterns for deploying Java functions to AWS Lambda using either Micronaut (full-featured, <1s cold start) or Raw Java (minimal overhead, <500ms cold start). Use when building serverless Java applications, optimizing cold starts, or configuring API Gateway/ALB integration.
- Choose between Micronaut framework and Raw Java approaches based on complexity and performance needs
- Optimize cold starts below 1 second with connection pooling, static initialization, and dependency management
- Configure API Gateway and ALB integration with request/response handling patterns
- Implement error handling, logging, and monitoring with CloudWatch and X-Ray
- Deploy using Serverless Framework or AWS SAM with CI/CD pipeline templates
- Package Java functions efficiently using Gradle Shadow or Maven Shade plugins
How to install aws-lambda-java-integration
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill aws-lambda-java-integration- Java 17 or 21 runtime installed locally
- Gradle or Maven build tool configured
- AWS account with Lambda, API Gateway, and IAM permissions
- AWS SAM CLI or Serverless Framework installed for local testing and deployment
How to use aws-lambda-java-integration
- 1.Choose your approach: Micronaut for complex apps with DI, or Raw Java for minimal overhead
- 2.Set up project structure with build.gradle/pom.xml and Handler class following the provided templates
- 3.Implement your handler using either MicronautRequestHandler or RequestHandler interface
- 4.Configure serverless.yml or SAM template.yaml with memory (512MB+), timeout (5-30s), and events
- 5.Build and package your function using Shadow/Shade plugin to create optimized JAR
- 6.Test locally using `sam local invoke` or `serverless invoke local` before deployment
- 7.Deploy using `serverless deploy` or `sam deploy` with `--stage dev` for initial testing
- 8.Monitor cold vs warm starts using CloudWatch Insights and X-Ray tracing
Use cases
- Deploying a REST API backend using Micronaut Lambda with dependency injection and AOT compilation
- Reducing cold start time from 3 seconds to under 500ms by moving AWS client initialization to static fields
- Setting up GitHub Actions CI/CD pipeline to build, test, and deploy Java Lambda functions across dev/staging/prod
- Configuring provisioned concurrency and memory optimization for high-traffic serverless applications
- Building lightweight event handlers with Raw Java for simple request processing with minimal overhead
- Java backend developers building serverless applications
- DevOps engineers optimizing Lambda performance and cold starts
- Teams migrating Spring Boot or enterprise Java applications to Lambda
- Developers choosing between framework-based and minimal-overhead Lambda approaches
aws-lambda-java-integration FAQ
Micronaut provides a full framework with dependency injection, AOT compilation, and cold starts <1s, best for complex applications. Raw Java is minimal overhead with cold starts <500ms, ideal for simple handlers. Choose based on complexity needs and cold start requirements.
Move AWS client creation to static fields for reuse across invocations, minimize dependencies in your build, use AOT compilation (Micronaut), increase memory allocation, and consider provisioned concurrency for predictable traffic.
Start with 512MB and adjust based on profiling. Java requires more memory than Python/Node.js. Use CloudWatch metrics to measure CPU and duration, then optimize. Higher memory also increases CPU allocation.
Spring Boot adds significant cold start overhead (3-5s+). For Lambda, prefer Micronaut (optimized for serverless) or Raw Java. If you must use Spring, use Spring Cloud Function with native compilation.
Use `sam local invoke` with SAM templates or `serverless invoke local` with Serverless Framework. Both allow testing handlers locally with mock events before deploying to AWS.
Full instructions (SKILL.md)
Source of truth, from giuseppe-trisciuoglio/developer-kit.
name: aws-lambda-java-integration description: Provides AWS Lambda integration patterns for Java with cold start optimization. Use when deploying Java functions to AWS Lambda, choosing between Micronaut and Raw Java approaches, optimizing cold starts below 1 second, configuring API Gateway or ALB integration, or implementing serverless Java applications. Triggers include "create lambda java", "deploy java lambda", "micronaut lambda aws", "java lambda cold start", "aws lambda java performance", "java serverless framework". allowed-tools: Read, Write, Edit, Bash, Glob, Grep
AWS Lambda Java Integration
Patterns for creating high-performance AWS Lambda functions in Java with optimized cold starts.
Overview
This skill provides complete patterns for AWS Lambda Java development, covering two main approaches:
- Micronaut Framework - Full-featured framework with AOT compilation, dependency injection, and cold start < 1s
- Raw Java - Minimal overhead approach with cold start < 500ms
Both approaches support API Gateway and ALB integration with production-ready configurations.
When to Use
- Deploying Java functions to AWS Lambda
- Optimizing cold starts below 1 second
- Choosing between Micronaut and Raw Java approaches
- Configuring API Gateway or ALB integration
- Setting up CI/CD pipelines for Java Lambda
Instructions
1. Choose Your Approach
| Approach | Cold Start | Best For | Complexity |
|---|---|---|---|
| Micronaut | < 1s | Complex apps, DI needed, enterprise | Medium |
| Raw Java | < 500ms | Simple handlers, minimal overhead | Low |
Validate: Confirm the approach fits your use case before proceeding.
2. Project Structure
my-lambda-function/
├── build.gradle (or pom.xml)
├── src/main/java/com/example/Handler.java
└── serverless.yml (or template.yaml)
Validate: Verify project structure matches the template.
3. Implementation Examples
Micronaut Handler
@FunctionBean("my-function")
public class MyFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final MyService service;
public MyFunction(MyService service) {
this.service = service;
}
@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent request) {
// Process request
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}
Raw Java Handler
public class MyHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private static final MyService service = new MyService();
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}
Validate: Run sam local invoke to verify handler works before deployment.
Core Patterns
Connection Management
// Initialize once, reuse across invocations
private static final DynamoDbClient dynamoDb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
// Avoid: Creating clients in handler (slow on every invocation)
Error Handling
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
try {
return successResponse(process(request));
} catch (ValidationException e) {
return errorResponse(400, e.getMessage());
} catch (Exception e) {
context.getLogger().log("Error: " + e.getMessage());
return errorResponse(500, "Internal error");
}
}
Best Practices
Configuration
- Memory: Start with 512MB, adjust based on profiling
- Timeout: Micronaut 10-30s, Raw Java 5-10s
- Runtime: Java 17 or 21 for best performance
Packaging
- Use Gradle Shadow Plugin or Maven Shade Plugin
- Exclude unnecessary dependencies
Monitoring
- Enable X-Ray tracing for performance analysis
- Use CloudWatch Insights to track cold vs warm starts
Deployment Options
Serverless Framework
service: my-java-lambda
provider:
name: aws
runtime: java21
memorySize: 512
timeout: 10
package:
artifact: build/libs/function.jar
functions:
api:
handler: com.example.Handler
events:
- http:
path: /{proxy+}
method: ANY
Validate: Run serverless deploy with --stage dev first.
AWS SAM
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: build/libs/function.jar
Handler: com.example.Handler
Runtime: java21
MemorySize: 512
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
Validate: Run sam validate before deploying.
Constraints and Warnings
Java-Specific Constraints
- Reflection: Minimize use; prefer AOT compilation (Micronaut)
- Classpath scanning: Slows cold start; use explicit configuration
- Large frameworks: Spring Boot adds significant cold start overhead
Common Pitfalls
- Initialization in handler - Causes repeated work on warm invocations
- Oversized JARs - Include only required dependencies
- Insufficient memory - Java needs more memory than Node.js/Python
- No timeout handling - Always set appropriate timeouts
References
For detailed guidance on specific topics:
- Micronaut Lambda - Complete Micronaut setup, AOT configuration, DI optimization
- Raw Java Lambda - Minimal handler patterns, singleton caching, JAR packaging
- Serverless Deployment - Serverless Framework, SAM, CI/CD pipelines, provisioned concurrency
- Testing Lambda - JUnit 5, SAM Local, integration testing, performance measurement
Examples
Example 1: Create a Micronaut Lambda Function
Input:
Create a Java Lambda function using Micronaut to handle user REST API
Process:
- Configure Gradle project with Micronaut plugin
- Create Handler class extending MicronautRequestHandler
- Implement methods for GET/POST/PUT/DELETE
- Configure application.yml with AOT optimizations
- Set up packaging with Shadow plugin
- Validate: Test locally with SAM CLI before deploying
Output:
- Complete project structure
- Handler with dependency injection
- serverless.yml deployment configuration
Example 2: Optimize Cold Start for Raw Java
Input:
My Java Lambda has 3 second cold start, how do I optimize it?
Process:
- Analyze initialization code
- Move AWS client creation to static fields
- Reduce dependencies in build.gradle
- Configure optimized JVM options
- Consider provisioned concurrency
- Validate: Measure cold start with CloudWatch metrics after changes
Output:
- Refactored code with singleton pattern
- Minimized JAR
- Cold start < 500ms
Example 3: Deploy with GitHub Actions
Input:
Configure CI/CD for Java Lambda with SAM
Process:
- Create GitHub Actions workflow
- Configure Gradle build with Shadow
- Set up SAM build and deploy
- Add test stage before deployment
- Configure environment protection for prod
Output:
- Complete .github/workflows/deploy.yml
- Multi-stage pipeline (dev/staging/prod)
- Integrated test automation
Version
Version: 1.0.0
Related skills
More from giuseppe-trisciuoglio/developer-kit and the wider catalog.

aws-lambda-php-integration
Deploy PHP and Symfony apps to AWS Lambda with Bref framework, optimized for cold starts and serverless patterns.

aws-lambda-python-integration
AWS Lambda Python integration with cold start optimization for Chalice and raw Python approaches.

aws-lambda-typescript-integration
AWS Lambda integration patterns for TypeScript with cold start optimization and NestJS/raw TypeScript approaches.

aws-rds-spring-boot-integration
Configure AWS RDS (Aurora, MySQL, PostgreSQL) with Spring Boot using HikariCP pooling, SSL, IAM auth, and Secrets Manager.

aws-sdk-java-v2-bedrock
Invoke Claude, Llama, and Titan models via AWS SDK for Java 2.x with streaming, embeddings, and Spring Boot integration.

aws-sdk-java-v2-core
AWS SDK for Java 2.x client setup with credential resolution, HTTP tuning, timeouts, retries, and testing patterns.