Skip to content

giuseppe-trisciuoglio/developer-kit / plugins/developer-kit-java/skills/aws-sdk-java-v2-lambda

aws-sdk-java-v2-lambda

Unverified

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.

references/invocation-patterns.md

View on GitHub
# Lambda Invocation Patterns

## Synchronous Invocation

### Basic Invocation

```java
import software.amazon.awssdk.services.lambda.model.InvokeRequest;
import software.amazon.awssdk.services.lambda.model.InvokeResponse;
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();
}
```

### With JSON Objects

```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

```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);
}
```

## Asynchronous Invocation

### Fire-and-Forget

```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());
}
```

### Async with Event

```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);
    }
}
```

## Error Handling

### Comprehensive Error Handling

```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;
    }
}
```

### Custom Exception

```java
public class LambdaInvocationException extends RuntimeException {
    private final String functionName;
    private final String statusCode;

    public LambdaInvocationException(String message) {
        super(message);
        this.functionName = null;
        this.statusCode = null;
    }

    public LambdaInvocationException(String message, Throwable cause) {
        super(message, cause);
        this.functionName = null;
        this.statusCode = null;
    }

    public LambdaInvocationException(String functionName, String statusCode, String message) {
        super(message);
        this.functionName = functionName;
        this.statusCode = statusCode;
    }

    public String getFunctionName() {
        return functionName;
    }

    public String getStatusCode() {
        return statusCode;
    }
}
```

## Advanced Patterns

### Retry Logic

```java
public String invokeWithRetry(LambdaClient lambdaClient,
                              String functionName,
                              String payload,
                              int maxRetries) {
    int attempts = 0;

    while (attempts < maxRetries) {
        try {
            return invokeLambda(lambdaClient, functionName, payload);
        } catch (LambdaException e) {
            attempts++;
            if (attempts >= maxRetries) {
                throw e;
            }

            try {
                Thread.sleep(1000L * attempts); // Exponential backoff
            } catch (InterruptedException ie) {
                Thread.currentThread().interrupt();
                throw new RuntimeException("Interrupted during retry", ie);
            }
        }
    }

    throw new RuntimeException("Max retries exceeded");
}
```

### Batch Invocation

```java
public List<String> invokeBatch(LambdaClient lambdaClient,
                                String functionName,
                                List<String> payloads) {
    List<String> responses = new ArrayList<>();

    for (String payload : payloads) {
        try {
            String response = invokeLambda(lambdaClient, functionName, payload);
            responses.add(response);
        } catch (Exception e) {
            responses.add("ERROR: " + e.getMessage());
        }
    }

    return responses;
}
```

### Parallel Invocation

```java
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

public List<String> invokeParallel(LambdaAsyncClient asyncClient,
                                   String functionName,
                                   List<String> payloads) {
    List<CompletableFuture<String>> futures = payloads.stream()
        .map(payload -> CompletableFuture.supplyAsync(() -> {
            try {
                return invokeLambdaAsync(asyncClient, functionName, payload);
            } catch (Exception e) {
                return "ERROR: " + e.getMessage();
            }
        }))
        .collect(Collectors.toList());

    return futures.stream()
        .map(CompletableFuture::join)
        .collect(Collectors.toList());
}
```
writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deploymentStars: 318kUpdated Claude CodeCodexUnverifiedInstall
xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved.Stars: 195kUpdated Claude CodeCodexVerifiedInstall
shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy.Stars: 118kUpdated Claude CodeCodexVerifiedInstall
childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release.Stars: 117kUpdated Claude CodeCodexUnverifiedInstall