home / skills / giuseppe-trisciuoglio / developer-kit / aws-sdk-java-v2-lambda
This skill helps you manage and invoke AWS Lambda functions using Java SDK 2.x, enabling deployment, configuration, and Spring Boot integration.
npx playbooks add skill giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-lambdaReview the files below or copy the command above to add this skill to your agents.
---
name: aws-sdk-java-v2-lambda
description: Provides AWS Lambda patterns using AWS SDK for Java 2.x. Use when invoking Lambda functions, creating/updating functions, managing function configurations, working with Lambda layers, or integrating Lambda with Spring Boot applications.
category: aws
tags: [aws, lambda, java, sdk, serverless, functions]
version: 1.1.0
allowed-tools: Read, Write, Edit, Bash, Glob, Grep
---
# AWS SDK for Java 2.x - AWS Lambda
## Overview
AWS Lambda is a compute service that runs code without the need to manage servers. Your code runs automatically, scaling up and down with pay-per-use pricing. Use this skill to implement AWS Lambda operations using AWS SDK for Java 2.x in applications and services.
## When to Use
Use this skill when:
- Invoking Lambda functions programmatically
- Creating or updating Lambda functions
- Managing Lambda function configurations
- Working with Lambda environment variables
- Managing Lambda layers and aliases
- Implementing asynchronous Lambda invocations
- Integrating Lambda with Spring Boot
## Instructions
Follow these steps to work with AWS Lambda:
1. **Add Dependencies** - Include Lambda SDK dependency
2. **Create Client** - Instantiate LambdaClient with proper configuration
3. **Invoke Functions** - Use invoke() with appropriate invocation type
4. **Handle Responses** - Parse response payloads and handle function errors
5. **Manage Functions** - Create, update, or delete Lambda functions
6. **Configure Environment** - Set environment variables and concurrency limits
7. **Integrate with Spring** - Configure Lambda beans and services
8. **Test Locally** - Use mocks or LocalStack for development testing
## Dependencies
```xml
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>lambda</artifactId>
</dependency>
```
## Client Setup
To use AWS Lambda, create a LambdaClient with the required region configuration:
```java
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.lambda.LambdaClient;
LambdaClient lambdaClient = LambdaClient.builder()
.region(Region.US_EAST_1)
.build();
```
For asynchronous operations, use LambdaAsyncClient:
```java
import software.amazon.awssdk.services.lambda.LambdaAsyncClient;
LambdaAsyncClient asyncLambdaClient = LambdaAsyncClient.builder()
.region(Region.US_EAST_1)
.build();
```
## Invoke Lambda Function
### Synchronous Invocation
Invoke Lambda functions synchronously to get immediate results:
```java
import software.amazon.awssdk.services.lambda.model.*;
import software.amazon.awssdk.core.SdkBytes;
public String invokeLambda(LambdaClient lambdaClient,
String functionName,
String payload) {
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(payload))
.build();
InvokeResponse response = lambdaClient.invoke(request);
return response.payload().asUtf8String();
}
```
### Asynchronous Invocation
Use asynchronous invocation for fire-and-forget scenarios:
```java
public void invokeLambdaAsync(LambdaClient lambdaClient,
String functionName,
String payload) {
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.invocationType(InvocationType.EVENT) // Asynchronous
.payload(SdkBytes.fromUtf8String(payload))
.build();
InvokeResponse response = lambdaClient.invoke(request);
System.out.println("Status: " + response.statusCode());
}
```
### Invoke with JSON Objects
Work with JSON payloads for complex data structures:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
public <T> String invokeLambdaWithObject(LambdaClient lambdaClient,
String functionName,
T requestObject) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String jsonPayload = mapper.writeValueAsString(requestObject);
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(jsonPayload))
.build();
InvokeResponse response = lambdaClient.invoke(request);
return response.payload().asUtf8String();
}
```
### Parse Typed Responses
Parse JSON responses into typed objects:
```java
public <T> T invokeLambdaAndParse(LambdaClient lambdaClient,
String functionName,
Object request,
Class<T> responseType) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String jsonPayload = mapper.writeValueAsString(request);
InvokeRequest invokeRequest = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(jsonPayload))
.build();
InvokeResponse response = lambdaClient.invoke(invokeRequest);
String responseJson = response.payload().asUtf8String();
return mapper.readValue(responseJson, responseType);
}
```
## Function Management
### List Functions
List all Lambda functions for the current account:
```java
public List<FunctionConfiguration> listFunctions(LambdaClient lambdaClient) {
ListFunctionsResponse response = lambdaClient.listFunctions();
return response.functions();
}
```
### Get Function Configuration
Retrieve function configuration and metadata:
```java
public FunctionConfiguration getFunctionConfig(LambdaClient lambdaClient,
String functionName) {
GetFunctionRequest request = GetFunctionRequest.builder()
.functionName(functionName)
.build();
GetFunctionResponse response = lambdaClient.getFunction(request);
return response.configuration();
}
```
### Update Function Code
Update Lambda function code with new deployment package:
```java
import java.nio.file.Files;
import java.nio.file.Paths;
public void updateFunctionCode(LambdaClient lambdaClient,
String functionName,
String zipFilePath) throws IOException {
byte[] zipBytes = Files.readAllBytes(Paths.get(zipFilePath));
UpdateFunctionCodeRequest request = UpdateFunctionCodeRequest.builder()
.functionName(functionName)
.zipFile(SdkBytes.fromByteArray(zipBytes))
.publish(true)
.build();
UpdateFunctionCodeResponse response = lambdaClient.updateFunctionCode(request);
System.out.println("Updated function version: " + response.version());
}
```
### Update Function Configuration
Modify function settings like timeout, memory, and environment variables:
```java
public void updateFunctionConfiguration(LambdaClient lambdaClient,
String functionName,
Map<String, String> environment) {
Environment env = Environment.builder()
.variables(environment)
.build();
UpdateFunctionConfigurationRequest request = UpdateFunctionConfigurationRequest.builder()
.functionName(functionName)
.environment(env)
.timeout(60)
.memorySize(512)
.build();
lambdaClient.updateFunctionConfiguration(request);
}
```
### Create Function
Create new Lambda functions with code and configuration:
```java
public void createFunction(LambdaClient lambdaClient,
String functionName,
String roleArn,
String handler,
String zipFilePath) throws IOException {
byte[] zipBytes = Files.readAllBytes(Paths.get(zipFilePath));
FunctionCode code = FunctionCode.builder()
.zipFile(SdkBytes.fromByteArray(zipBytes))
.build();
CreateFunctionRequest request = CreateFunctionRequest.builder()
.functionName(functionName)
.runtime(Runtime.JAVA17)
.role(roleArn)
.handler(handler)
.code(code)
.timeout(60)
.memorySize(512)
.build();
CreateFunctionResponse response = lambdaClient.createFunction(request);
System.out.println("Function ARN: " + response.functionArn());
}
```
### Delete Function
Remove Lambda functions when no longer needed:
```java
public void deleteFunction(LambdaClient lambdaClient, String functionName) {
DeleteFunctionRequest request = DeleteFunctionRequest.builder()
.functionName(functionName)
.build();
lambdaClient.deleteFunction(request);
}
```
## Spring Boot Integration
### Configuration
Configure Lambda clients as Spring beans:
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class LambdaConfiguration {
@Bean
public LambdaClient lambdaClient() {
return LambdaClient.builder()
.region(Region.US_EAST_1)
.build();
}
}
```
### Lambda Invoker Service
Create a service for Lambda function invocation:
```java
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
@Service
public class LambdaInvokerService {
private final LambdaClient lambdaClient;
private final ObjectMapper objectMapper;
@Autowired
public LambdaInvokerService(LambdaClient lambdaClient, ObjectMapper objectMapper) {
this.lambdaClient = lambdaClient;
this.objectMapper = objectMapper;
}
public <T, R> R invoke(String functionName, T request, Class<R> responseType) {
try {
String jsonPayload = objectMapper.writeValueAsString(request);
InvokeRequest invokeRequest = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(jsonPayload))
.build();
InvokeResponse response = lambdaClient.invoke(invokeRequest);
if (response.functionError() != null) {
throw new LambdaInvocationException(
"Lambda function error: " + response.functionError());
}
String responseJson = response.payload().asUtf8String();
return objectMapper.readValue(responseJson, responseType);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke Lambda function", e);
}
}
public void invokeAsync(String functionName, Object request) {
try {
String jsonPayload = objectMapper.writeValueAsString(request);
InvokeRequest invokeRequest = InvokeRequest.builder()
.functionName(functionName)
.invocationType(InvocationType.EVENT)
.payload(SdkBytes.fromUtf8String(jsonPayload))
.build();
lambdaClient.invoke(invokeRequest);
} catch (Exception e) {
throw new RuntimeException("Failed to invoke Lambda function async", e);
}
}
}
```
### Typed Lambda Client
Create type-safe interfaces for Lambda services:
```java
public interface OrderProcessor {
OrderResponse processOrder(OrderRequest request);
}
@Service
public class LambdaOrderProcessor implements OrderProcessor {
private final LambdaInvokerService lambdaInvoker;
@Value("${lambda.order-processor.function-name}")
private String functionName;
public LambdaOrderProcessor(LambdaInvokerService lambdaInvoker) {
this.lambdaInvoker = lambdaInvoker;
}
@Override
public OrderResponse processOrder(OrderRequest request) {
return lambdaInvoker.invoke(functionName, request, OrderResponse.class);
}
}
```
## Error Handling
Implement comprehensive error handling for Lambda operations:
```java
public String invokeLambdaSafe(LambdaClient lambdaClient,
String functionName,
String payload) {
try {
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(payload))
.build();
InvokeResponse response = lambdaClient.invoke(request);
// Check for function error
if (response.functionError() != null) {
String errorMessage = response.payload().asUtf8String();
throw new RuntimeException("Lambda error: " + errorMessage);
}
// Check status code
if (response.statusCode() != 200) {
throw new RuntimeException("Lambda invocation failed with status: " +
response.statusCode());
}
return response.payload().asUtf8String();
} catch (LambdaException e) {
System.err.println("Lambda error: " + e.awsErrorDetails().errorMessage());
throw e;
}
}
public class LambdaInvocationException extends RuntimeException {
public LambdaInvocationException(String message) {
super(message);
}
public LambdaInvocationException(String message, Throwable cause) {
super(message, cause);
}
}
```
## Examples
### Example 1: Basic Lambda Invocation
```java
public String invokeFunction(LambdaClient client, String functionName, String payload) {
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.payload(SdkBytes.fromUtf8String(payload))
.build();
InvokeResponse response = client.invoke(request);
if (response.functionError() != null) {
throw new RuntimeException("Lambda error: " + response.functionError());
}
return response.payload().asUtf8String();
}
```
### Example 2: Async Invocation
```java
public void invokeAsync(LambdaClient client, String functionName, Map<String, Object> event) {
try {
String jsonPayload = new ObjectMapper().writeValueAsString(event);
InvokeRequest request = InvokeRequest.builder()
.functionName(functionName)
.invocationType(InvocationType.EVENT)
.payload(SdkBytes.fromUtf8String(jsonPayload))
.build();
client.invoke(request);
} catch (Exception e) {
throw new RuntimeException("Async invocation failed", e);
}
}
```
### Example 3: Spring Boot Service
```java
@Service
public class LambdaService {
private final LambdaClient lambdaClient;
private final ObjectMapper objectMapper;
@Value("${lambda.user-processor-function}")
private String userProcessorFunction;
public UserResponse processUser(UserRequest request) {
try {
String payload = objectMapper.writeValueAsString(request);
InvokeResponse response = lambdaClient.invoke(
InvokeRequest.builder()
.functionName(userProcessorFunction)
.payload(SdkBytes.fromUtf8String(payload))
.build()
);
return objectMapper.readValue(
response.payload().asUtf8String(),
UserResponse.class
);
} catch (Exception e) {
throw new RuntimeException("User processing failed", e);
}
}
}
```
For comprehensive code examples, see the references section:
- **Basic examples** - Simple invocation patterns and function management
- **Spring Boot integration** - Complete Spring Boot configuration and service patterns
- **Testing examples** - Unit and integration test patterns
- **Advanced patterns** - Complex scenarios and best practices
## Constraints and Warnings
- **Payload Size**: Lambda payload limited to 6MB for sync invocation, 256KB for async
- **Timeout**: Maximum function timeout is 15 minutes
- **Memory**: Memory configuration affects CPU and network performance
- **Concurrency**: Account-level concurrency limits can cause throttling
- **Cold Starts**: New invocations may have delays for initialization
- **VPC**: VPC functions need proper security group and subnet configuration
- **Layers**: Lambda layers count towards deployment package size limit
- **Reserved Concurrency**: Prevents throttling but limits maximum scaling
- **Event Source Mappings**: Some event sources require special configuration
- **Cost**: Unexpected usage can lead to high bills; set budgets and alerts
## Best Practices
1. **Reuse Lambda clients**: Create once and reuse across invocations
2. **Set appropriate timeouts**: Match client timeout to Lambda function timeout
3. **Use async invocation**: For fire-and-forget scenarios
4. **Handle errors properly**: Check for function errors and status codes
5. **Use environment variables**: For function configuration
6. **Implement retry logic**: For transient failures
7. **Monitor invocations**: Use CloudWatch metrics
8. **Version functions**: Use aliases and versions for production
9. **Use VPC**: For accessing resources in private subnets
10. **Optimize payload size**: Keep payloads small for better performance
## Testing
Test Lambda services using mocks and test assertions:
```java
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
class LambdaInvokerServiceTest {
@Mock
private LambdaClient lambdaClient;
@Mock
private ObjectMapper objectMapper;
@InjectMocks
private LambdaInvokerService service;
@Test
void shouldInvokeLambdaSuccessfully() throws Exception {
// Test implementation
}
}
```
## Related Skills
- @aws-sdk-java-v2-core - Core AWS SDK patterns and client configuration
- @spring-boot-dependency-injection - Spring dependency injection best practices
- @unit-test-service-layer - Service testing patterns with Mockito
- @spring-boot-actuator - Production monitoring and health checks
## References
For detailed information and examples, see the following reference files:
- **[Official Documentation](references/official-documentation.md)** - AWS Lambda concepts, API reference, and official guidance
- **[Examples](references/examples.md)** - Complete code examples and integration patterns
## Additional Resources
- [Lambda Examples on GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/lambda)
- [Lambda API Reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/lambda/package-summary.html)
- [AWS Lambda Developer Guide](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)This skill provides ready-to-use patterns and examples for using AWS Lambda with the AWS SDK for Java 2.x. It includes client setup, synchronous and asynchronous invocation, function lifecycle management, environment and configuration handling, and Spring Boot integration. Use it to build type-safe, testable Lambda interactions in Java applications and services.
The skill supplies code patterns and step-by-step recipes that show how to create LambdaClient and LambdaAsyncClient, invoke functions with JSON payloads, parse typed responses, and handle errors. It also covers creating, updating, listing and deleting functions, managing environment variables and layers, and wiring Lambda clients and invokers as Spring beans. Examples include synchronous invokes, fire-and-forget async calls, update deployment packages, and typed service adapters for Spring.
Do I need special credentials to use the clients?
Yes. Configure AWS credentials and region via the SDK default provider chain, environment variables, or explicit builders before creating LambdaClient/LambdaAsyncClient.
When should I use LambdaAsyncClient instead of LambdaClient?
Use LambdaAsyncClient for non-blocking operations and higher concurrency. For simple synchronous requests where you need an immediate response, use LambdaClient.