$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-lambdaProvides 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.
| 1 | # AWS SDK for Java 2.x - AWS Lambda |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | AWS Lambda is a compute service that runs code without managing servers. Use this skill to implement AWS Lambda operations using AWS SDK for Java 2.x in applications and services. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Invoking Lambda functions from Java applications |
| 10 | - Deploying and updating Lambda functions via SDK |
| 11 | - Managing function configurations and layers |
| 12 | - Integrating Lambda with Spring Boot applications |
| 13 | |
| 14 | ## Quick Reference |
| 15 | |
| 16 | | Operation | SDK Method | Use Case | |
| 17 | |-----------|------------|----------| |
| 18 | | **Invoke** | `invoke()` | Synchronous/async function invocation | |
| 19 | | **List Functions** | `listFunctions()` | Get all Lambda functions | |
| 20 | | **Get Config** | `getFunction()` | Retrieve function configuration | |
| 21 | | **Create Function** | `createFunction()` | Create new Lambda function | |
| 22 | | **Update Code** | `updateFunctionCode()` | Deploy new function code | |
| 23 | | **Update Config** | `updateFunctionConfiguration()` | Modify settings (timeout, memory, env vars) | |
| 24 | | **Delete Function** | `deleteFunction()` | Remove Lambda function | |
| 25 | |
| 26 | ## Instructions |
| 27 | |
| 28 | ### 1. Add Dependencies |
| 29 | |
| 30 | Include Lambda SDK dependency in `pom.xml`: |
| 31 | |
| 32 | ```xml |
| 33 | <dependency> |
| 34 | <groupId>software.amazon.awssdk</groupId> |
| 35 | <artifactId>lambda</artifactId> |
| 36 | </dependency> |
| 37 | ``` |
| 38 | |
| 39 | See [client-setup.md](references/client-setup.md) for complete setup. |
| 40 | |
| 41 | ### 2. Create Client |
| 42 | |
| 43 | Instantiate `LambdaClient` with proper configuration: |
| 44 | |
| 45 | ```java |
| 46 | LambdaClient lambdaClient = LambdaClient.builder() |
| 47 | .region(Region.US_EAST_1) |
| 48 | .build(); |
| 49 | ``` |
| 50 | |
| 51 | For async operations, use `LambdaAsyncClient`. |
| 52 | |
| 53 | ### 3. Invoke Lambda Function |
| 54 | |
| 55 | Synchronous invocation: |
| 56 | |
| 57 | ```java |
| 58 | InvokeRequest request = InvokeRequest.builder() |
| 59 | .functionName("my-function") |
| 60 | .payload(SdkBytes.fromUtf8String(payload)) |
| 61 | .build(); |
| 62 | |
| 63 | InvokeResponse response = lambdaClient.invoke(request); |
| 64 | |
| 65 | return response.payload().asUtf8String(); |
| 66 | ``` |
| 67 | |
| 68 | See [invocation-patterns.md](references/invocation-patterns.md) for patterns. |
| 69 | |
| 70 | ### 4. Handle Responses |
| 71 | |
| 72 | Parse response payloads and check for errors: |
| 73 | |
| 74 | ```java |
| 75 | if (response.functionError() != null) { |
| 76 | throw new LambdaInvocationException("Lambda error: " + response.functionError()); |
| 77 | } |
| 78 | |
| 79 | String result = response.payload().asUtf8String(); |
| 80 | ``` |
| 81 | |
| 82 | ### 5. Manage Functions |
| 83 | |
| 84 | Create, update, or delete Lambda functions: |
| 85 | |
| 86 | ```java |
| 87 | // Create |
| 88 | CreateFunctionRequest createRequest = CreateFunctionRequest.builder() |
| 89 | .functionName("my-function") |
| 90 | .runtime(Runtime.JAVA17) |
| 91 | .role(roleArn) |
| 92 | .code(code) |
| 93 | .build(); |
| 94 | |
| 95 | lambdaClient.createFunction(createRequest); |
| 96 | |
| 97 | // Verify function is active before proceeding |
| 98 | GetFunctionRequest getRequest = GetFunctionRequest.builder() |
| 99 | .functionName("my-function") |
| 100 | .build(); |
| 101 | GetFunctionResponse getResponse = lambdaClient.getFunction(getRequest); |
| 102 | if (!"Active".equals(getResponse.configuration().state())) { |
| 103 | throw new IllegalStateException("Function not active: " + getResponse.configuration().stateReason()); |
| 104 | } |
| 105 | |
| 106 | // Update code |
| 107 | UpdateFunctionCodeRequest updateCodeRequest = UpdateFunctionCodeRequest.builder() |
| 108 | .functionName("my-function") |
| 109 | .zipFile(SdkBytes.fromByteArray(zipBytes)) |
| 110 | .build(); |
| 111 | |
| 112 | lambdaClient.updateFunctionCode(updateCodeRequest); |
| 113 | |
| 114 | // Wait for deployment to complete |
| 115 | Waiter<GetFunctionConfigurationRequest> waiter = lambdaClient.waiter(); |
| 116 | waiter.waitUntilFunctionUpdatedActive(GetFunctionConfigurationRequest.builder() |
| 117 | .functionName("my-function") |
| 118 | .build()); |
| 119 | ``` |
| 120 | |
| 121 | See [function-management.md](references/function-management.md) for complete patterns. |
| 122 | |
| 123 | ### 6. Configure Environment |
| 124 | |
| 125 | Set environment variables and concurrency limits: |
| 126 | |
| 127 | ```java |
| 128 | Environment env = Environment.builder() |
| 129 | .variables(Map.of( |
| 130 | "DB_URL", "jdbc:postgresql://db", |
| 131 | "LOG_LEVEL", "INFO" |
| 132 | )) |
| 133 | .build(); |
| 134 | |
| 135 | UpdateFunctionConfigurationRequest configRequest = UpdateFunctionConfigurationRequest.builder() |
| 136 | .functionName("my-function") |
| 137 | .environment(env) |
| 138 | .timeout(60) |
| 139 | .memorySize(512) |
| 140 | .build(); |
| 141 | |
| 142 | lambdaClient.updateFunctionConfiguration(configRequest); |
| 143 | ``` |
| 144 | |
| 145 | ### 7. Integrate with Spring Boot |
| 146 | |
| 147 | Configure Lambda beans and services: |
| 148 | |
| 149 | ```java |
| 150 | @Configuration |
| 151 | public class LambdaConfiguration { |
| 152 | @Bean |
| 153 | public LambdaClient lambdaClient() { |
| 154 | return LambdaClient.builder() |
| 155 | .region(Region.US_EAST_1) |
| 156 | .build(); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | @Service |
| 161 | public class LambdaInvokerService { |
| 162 | public <T, R> R invoke(String functionName, T request, Class<R> responseType) { |
| 163 | // Implementation |
| 164 | } |
| 165 | } |
| 166 | ``` |
| 167 | |
| 168 | See [spring-boot-integration.md](references/spring-boot-integration.md) for complete integration. |
| 169 | |
| 170 | ### 8. Test Locally |
| 171 | |
| 172 | Use mocks or LocalStack for development testing. |
| 173 | |
| 174 | Se |