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".
Patterns for creating high-performance AWS Lambda functions in Java with optimized cold starts.
This skill provides complete patterns for AWS Lambda Java development, covering two main approaches:
Both approaches support API Gateway and ALB integration with production-ready configurations.
Use this skill when:
| Approach | Cold Start | Best For | Complexity |
|---|---|---|---|
| Micronaut | < 1s | Complex apps, DI needed, enterprise | Medium |
| Raw Java | < 500ms | Simple handlers, minimal overhead | Low |
my-lambda-function/
├── build.gradle (or pom.xml)
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/
│ │ └── Handler.java
│ └── resources/
│ └── application.yml (Micronaut only)
└── serverless.yml (or template.yaml)
@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\"}");
}
}
public class MyHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
// Singleton pattern for warm invocations
private static final MyService service = new MyService();
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}
Cold start time depends on initialization code. Key strategies:
// GOOD: Initialize once, reuse across invocations
private static final DynamoDbClient dynamoDb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
// AVOID: Creating clients in handler method
public APIGatewayProxyResponseEvent handleRequest(...) {
DynamoDbClient client = DynamoDbClient.create(); // Slow on every invocation
}
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
try {
// Business logic
return successResponse(result);
} catch (ValidationException e) {
return errorResponse(400, e.getMessage());
} catch (Exception e) {
context.getLogger().log("Error: " + e.getMessage());
return errorResponse(500, "Internal error");
}
}