aws-lambda-java-integration by giuseppe-trisciuoglio/developer-kit
npx skills add https://github.com/giuseppe-trisciuoglio/developer-kit --skill aws-lambda-java-integration使用 Java 创建高性能 AWS Lambda 函数的模式,并优化冷启动时间。
此技能提供了 AWS Lambda Java 开发的完整模式,涵盖两种主要方法:
两种方法都支持 API Gateway 和 ALB 集成,并提供生产就绪的配置。
在以下情况下使用此技能:
| 方法 | 冷启动时间 | 最佳适用场景 | 复杂度 |
|---|---|---|---|
| Micronaut | < 1 秒 | 复杂应用、需要依赖注入、企业级 | 中等 |
| 原生 Java | < 500 毫秒 | 简单的处理器、最小化开销 | 低 |
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
my-lambda-function/
├── build.gradle (或 pom.xml)
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/
│ │ └── Handler.java
│ └── resources/
│ └── application.yml (仅 Micronaut 需要)
└── serverless.yml (或 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) {
// 处理请求
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}
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\"}");
}
}
冷启动时间取决于初始化代码。关键策略:
// 良好实践:初始化一次,在多次调用中复用
private static final DynamoDbClient dynamoDb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
// 避免:在处理器方法中创建客户端
public APIGatewayProxyResponseEvent handleRequest(...) {
DynamoDbClient client = DynamoDbClient.create(); // 每次调用都慢
}
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
try {
// 业务逻辑
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");
}
}
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
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
有关特定主题的详细指导:
输入:
Create a Java Lambda function using Micronaut to handle user REST API
处理过程:
MicronautRequestHandler 的 Handler 类application.yml输出:
serverless.yml 部署配置输入:
My Java Lambda has 3 second cold start, how do I optimize it?
处理过程:
build.gradle 中的依赖项输出:
输入:
Configure CI/CD for Java Lambda with SAM
处理过程:
输出:
.github/workflows/deploy.yml版本:1.0.0
每周安装数
188
代码仓库
GitHub 星标数
173
首次出现
2026年2月20日
安全审计
安装于
codex167
gemini-cli165
github-copilot162
opencode160
cursor160
kimi-cli159
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");
}
}
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
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
For detailed guidance on specific topics:
Input:
Create a Java Lambda function using Micronaut to handle user REST API
Process:
Output:
Input:
My Java Lambda has 3 second cold start, how do I optimize it?
Process:
Output:
Input:
Configure CI/CD for Java Lambda with SAM
Process:
Output:
Version: 1.0.0
Weekly Installs
188
Repository
GitHub Stars
173
First Seen
Feb 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex167
gemini-cli165
github-copilot162
opencode160
cursor160
kimi-cli159
Azure RBAC 权限管理工具:查找最小角色、创建自定义角色与自动化分配
131,500 周安装