Running Java on AWS Lambda is straightforward with the managed runtime, and SnapStart dramatically improves cold starts for certain workloads.
Handler Interface
Implement RequestHandler for API Gateway proxy events:
public class HelloHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
@Override
public APIGatewayProxyResponseEvent handle(APIGatewayProxyRequestEvent input, Context context) {
var response = new APIGatewayProxyResponseEvent();
response.setStatusCode(200);
response.setBody("{\"message\":\"Hello from Lambda\"}");
return response;
}
}SAM Template with SnapStart
Enable SnapStart in your CloudFormation or SAM template:
Resources:
HelloFunction:
Type: AWS::Serverless::Function
Properties:
Runtime: java21
Handler: com.example.HelloHandler::handleRequest
SnapStart:
ApplyOn: PublishedVersions
MemorySize: 512
Timeout: 30When SnapStart Helps
SnapStart snapshots initialized JVM state after startup. It works best for frameworks with heavy classpath initialization—measure p99 latency before and after enabling it in your region.