$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-lambda-java-integrationProvides 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
| 1 | # AWS Lambda Java Integration |
| 2 | |
| 3 | Patterns for creating high-performance AWS Lambda functions in Java with optimized cold starts. |
| 4 | |
| 5 | ## Overview |
| 6 | |
| 7 | This skill provides complete patterns for AWS Lambda Java development, covering two main approaches: |
| 8 | |
| 9 | 1. **Micronaut Framework** - Full-featured framework with AOT compilation, dependency injection, and cold start < 1s |
| 10 | 2. **Raw Java** - Minimal overhead approach with cold start < 500ms |
| 11 | |
| 12 | Both approaches support API Gateway and ALB integration with production-ready configurations. |
| 13 | |
| 14 | ## When to Use |
| 15 | |
| 16 | - Deploying Java functions to AWS Lambda |
| 17 | - Optimizing cold starts below 1 second |
| 18 | - Choosing between Micronaut and Raw Java approaches |
| 19 | - Configuring API Gateway or ALB integration |
| 20 | - Setting up CI/CD pipelines for Java Lambda |
| 21 | |
| 22 | ## Instructions |
| 23 | |
| 24 | ### 1. Choose Your Approach |
| 25 | |
| 26 | | Approach | Cold Start | Best For | Complexity | |
| 27 | |----------|------------|----------|------------| |
| 28 | | Micronaut | < 1s | Complex apps, DI needed, enterprise | Medium | |
| 29 | | Raw Java | < 500ms | Simple handlers, minimal overhead | Low | |
| 30 | |
| 31 | **Validate**: Confirm the approach fits your use case before proceeding. |
| 32 | |
| 33 | ### 2. Project Structure |
| 34 | |
| 35 | ``` |
| 36 | my-lambda-function/ |
| 37 | ├── build.gradle (or pom.xml) |
| 38 | ├── src/main/java/com/example/Handler.java |
| 39 | └── serverless.yml (or template.yaml) |
| 40 | ``` |
| 41 | |
| 42 | **Validate**: Verify project structure matches the template. |
| 43 | |
| 44 | ### 3. Implementation Examples |
| 45 | |
| 46 | #### Micronaut Handler |
| 47 | |
| 48 | ```java |
| 49 | @FunctionBean("my-function") |
| 50 | public class MyFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 51 | |
| 52 | private final MyService service; |
| 53 | |
| 54 | public MyFunction(MyService service) { |
| 55 | this.service = service; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent request) { |
| 60 | // Process request |
| 61 | return new APIGatewayProxyResponseEvent() |
| 62 | .withStatusCode(200) |
| 63 | .withBody("{\"message\": \"Success\"}"); |
| 64 | } |
| 65 | } |
| 66 | ``` |
| 67 | |
| 68 | #### Raw Java Handler |
| 69 | |
| 70 | ```java |
| 71 | public class MyHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { |
| 72 | |
| 73 | private static final MyService service = new MyService(); |
| 74 | |
| 75 | @Override |
| 76 | public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) { |
| 77 | return new APIGatewayProxyResponseEvent() |
| 78 | .withStatusCode(200) |
| 79 | .withBody("{\"message\": \"Success\"}"); |
| 80 | } |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | **Validate**: Run `sam local invoke` to verify handler works before deployment. |
| 85 | |
| 86 | ## Core Patterns |
| 87 | |
| 88 | ### Connection Management |
| 89 | |
| 90 | ```java |
| 91 | // Initialize once, reuse across invocations |
| 92 | private static final DynamoDbClient dynamoDb = DynamoDbClient.builder() |
| 93 | .region(Region.US_EAST_1) |
| 94 | .build(); |
| 95 | |
| 96 | // Avoid: Creating clients in handler (slow on every invocation) |
| 97 | ``` |
| 98 | |
| 99 | ### Error Handling |
| 100 | |
| 101 | ```java |
| 102 | @Override |
| 103 | public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) { |
| 104 | try { |
| 105 | return successResponse(process(request)); |
| 106 | } catch (ValidationException e) { |
| 107 | return errorResponse(400, e.getMessage()); |
| 108 | } catch (Exception e) { |
| 109 | context.getLogger().log("Error: " + e.getMessage()); |
| 110 | return errorResponse(500, "Internal error"); |
| 111 | } |
| 112 | } |
| 113 | ``` |
| 114 | |
| 115 | ## Best Practices |
| 116 | |
| 117 | ### Configuration |
| 118 | |
| 119 | - **Memory**: Start with 512MB, adjust based on profiling |
| 120 | - **Timeout**: Micronaut 10-30s, Raw Java 5-10s |
| 121 | - **Runtime**: Java 17 or 21 for best performance |
| 122 | |
| 123 | ### Packaging |
| 124 | |
| 125 | - Use Gradle Shadow Plugin or Maven Shade Plugin |
| 126 | - Exclude unnecessary dependencies |
| 127 | |
| 128 | ### Monitoring |
| 129 | |
| 130 | - Enable X-Ray tracing for performance analysis |
| 131 | - Use CloudWatch Insights to track cold vs warm starts |
| 132 | |
| 133 | ## Deployment Options |
| 134 | |
| 135 | ### Serverless Framework |
| 136 | |
| 137 | ```yaml |
| 138 | service: my-java-lambda |
| 139 | provider: |
| 140 | name: aws |
| 141 | runtime: java21 |
| 142 | memorySize: 512 |
| 143 | timeout: 10 |
| 144 | package: |
| 145 | artifact: build/libs/function.jar |
| 146 | functions: |
| 147 | api: |
| 148 | handler: com.example.Handler |
| 149 | events: |
| 150 | - http: |
| 151 | path: /{proxy+} |
| 152 | method: ANY |
| 153 | ``` |
| 154 | |
| 155 | **Validate**: Run `serverless deploy` with `--stage dev` first. |
| 156 | |
| 157 | ### AWS SAM |
| 158 | |
| 159 | ```yaml |
| 160 | AWSTemplateFormatVersion: '2010-09-09' |
| 161 | Transform: AWS::Serverless-2016-10-31 |
| 162 | Resources: |
| 163 | MyFunction: |
| 164 | Type: AWS::Serverless::Function |
| 165 | Properties: |
| 166 | CodeUri: build/libs/function.jar |
| 167 | Handler: com.example.Handler |
| 168 | Runtime: java21 |
| 169 | MemorySize: 512 |
| 170 | Timeout: 10 |
| 171 | Events: |
| 172 | ApiEvent: |
| 173 | Type: Api |