|
1 | 1 | # AWS Lambda Durable Execution SDK Examples |
2 | 2 |
|
3 | | -This module contains example applications demonstrating how to use the AWS Lambda Durable Execution SDK for Java. |
| 3 | +Example applications demonstrating the AWS Lambda Durable Execution SDK for Java. |
4 | 4 |
|
5 | | -# Running Examples with JUnit Tests |
| 5 | +## Prerequisites |
6 | 6 |
|
7 | | -The examples demonstrate both local and cloud testing approaches using the SDK's testing utilities: |
| 7 | +- Java 17+ |
| 8 | +- Maven 3.8+ |
| 9 | +- AWS SAM CLI (for deployment) |
| 10 | +- Docker (for SAM build) |
| 11 | +- AWS credentials configured |
8 | 12 |
|
9 | | -## Local Testing (Fast, No AWS Required) |
| 13 | +## Local Testing |
10 | 14 |
|
11 | | -Use `LocalDurableTestRunner` for fast, in-memory testing without deploying to AWS Lambda: |
| 15 | +Run examples locally without AWS using `LocalDurableTestRunner`: |
12 | 16 |
|
13 | 17 | ```bash |
14 | | -# Run all example tests |
| 18 | +# First, build and install the SDK to local Maven repo (from project root) |
| 19 | +mvn clean install -DskipTests |
| 20 | + |
| 21 | +cd examples |
| 22 | + |
| 23 | +# Run all tests |
15 | 24 | mvn test |
16 | 25 |
|
17 | 26 | # Run specific test |
18 | 27 | mvn test -Dtest=SimpleStepExampleTest |
19 | 28 | ``` |
20 | 29 |
|
21 | | -The local runner skips actual wait times and runs entirely in-memory, making tests fast and suitable for CI/CD pipelines. |
| 30 | +The local runner executes in-memory and skips wait durations—ideal for fast iteration and CI/CD. |
22 | 31 |
|
23 | | -## Cloud Testing (End-to-End) |
| 32 | +## Deploy to AWS |
24 | 33 |
|
25 | | -Use `CloudDurableTestRunner` for end-to-end testing against deployed Lambda functions: |
| 34 | +```bash |
| 35 | +cd examples |
| 36 | +mvn clean package |
| 37 | +sam build |
| 38 | +sam deploy --guided |
| 39 | +``` |
26 | 40 |
|
27 | | -```java |
28 | | -var runner = CloudDurableTestRunner.create( |
29 | | - "arn:aws:lambda:us-east-1:123456789012:function:MyFunction", |
30 | | - MyInput.class, |
31 | | - MyOutput.class); |
| 41 | +On first deploy, SAM will prompt for stack name and region. Subsequent deploys use saved config: |
32 | 42 |
|
33 | | -var result = runner.run(new MyInput("test-data")); |
| 43 | +```bash |
| 44 | +sam deploy |
34 | 45 | ``` |
35 | 46 |
|
36 | | -This approach tests the actual deployed function, including all AWS integrations and durable execution behavior. |
| 47 | +The SAM template configures: |
| 48 | +- `DurableConfig` with `ExecutionTimeout` and `RetentionPeriodInDays` |
| 49 | +- IAM permissions for `lambda:CheckpointDurableExecutions` and `lambda:GetDurableExecutionState` |
37 | 50 |
|
38 | | -# Running Examples with AWS SAM locally |
| 51 | +## Invoke Deployed Functions |
39 | 52 |
|
40 | | -**Build the project:** |
41 | | - ```bash |
42 | | - mvn clean package |
43 | | - ``` |
| 53 | +```bash |
| 54 | +sam remote invoke SimpleStepExampleFunction \ |
| 55 | + --event '{"name":"World"}' \ |
| 56 | + --stack-name durable-sdk-examples |
| 57 | +``` |
44 | 58 |
|
45 | | -### Option 1: Local Lambda Endpoint |
| 59 | +## Cloud Integration Tests |
46 | 60 |
|
47 | | -Start a local Lambda endpoint: |
| 61 | +Run tests against deployed functions using `CloudDurableTestRunner`: |
48 | 62 |
|
49 | 63 | ```bash |
50 | | -sam local start-lambda |
| 64 | +cd examples |
| 65 | +mvn test -Dtest=CloudBasedIntegrationTest -Dtest.cloud.enabled=true |
51 | 66 | ``` |
52 | 67 |
|
53 | | -In another terminal, invoke the function: |
| 68 | +The tests auto-detect your AWS account and region from credentials. Override if needed: |
54 | 69 |
|
55 | 70 | ```bash |
56 | | -# Invoke SimpleStepExample |
57 | | -aws lambda invoke \ |
58 | | - --function-name SimpleStepExampleFunction \ |
59 | | - --endpoint-url http://127.0.0.1:3001 \ |
60 | | - --payload '{"name":"Alice"}' \ |
61 | | - --cli-binary-format raw-in-base64-out \ |
62 | | - response.json |
63 | | - |
64 | | -# Invoke WaitExample |
65 | | -aws lambda invoke \ |
66 | | - --function-name WaitExampleFunction \ |
67 | | - --endpoint-url http://127.0.0.1:3001 \ |
68 | | - --payload '{"name":"Bob"}' \ |
69 | | - --cli-binary-format raw-in-base64-out \ |
70 | | - response.json |
| 71 | +mvn test -Dtest=CloudBasedIntegrationTest \ |
| 72 | + -Dtest.cloud.enabled=true \ |
| 73 | + -Dtest.aws.account=123456789012 \ |
| 74 | + -Dtest.aws.region=us-east-1 |
71 | 75 | ``` |
72 | 76 |
|
73 | | -### Option 2: Direct Invocation |
| 77 | +## Examples |
74 | 78 |
|
75 | | -Invoke the function directly: |
| 79 | +| Example | Description | |
| 80 | +|---------|-------------| |
| 81 | +| [SimpleStepExample](src/main/java/com/amazonaws/lambda/durable/examples/SimpleStepExample.java) | Basic sequential steps | |
| 82 | +| [WaitExample](src/main/java/com/amazonaws/lambda/durable/examples/WaitExample.java) | Suspend execution with `wait()` | |
| 83 | +| [RetryExample](src/main/java/com/amazonaws/lambda/durable/examples/RetryExample.java) | Configuring retry strategies | |
| 84 | +| [GenericTypesExample](src/main/java/com/amazonaws/lambda/durable/examples/GenericTypesExample.java) | Working with `List<T>` and `Map<K,V>` | |
| 85 | +| [CustomConfigExample](src/main/java/com/amazonaws/lambda/durable/examples/CustomConfigExample.java) | Custom Lambda client and SerDes | |
| 86 | +| [WaitAtLeastExample](src/main/java/com/amazonaws/lambda/durable/examples/WaitAtLeastExample.java) | Concurrent `stepAsync()` with `wait()` | |
| 87 | +| [RetryInProcessExample](src/main/java/com/amazonaws/lambda/durable/examples/RetryInProcessExample.java) | In-process retry with concurrent operations | |
| 88 | +| [WaitAtLeastInProcessExample](src/main/java/com/amazonaws/lambda/durable/examples/WaitAtLeastInProcessExample.java) | Wait completes before async step (no suspension) | |
76 | 89 |
|
77 | | -```bash |
78 | | -sam local invoke SimpleStepExampleFunction \ |
79 | | - --event event.json |
80 | | -``` |
| 90 | +## Cleanup |
81 | 91 |
|
82 | | -Create `event.json`: |
83 | | -```json |
84 | | -{ |
85 | | - "name": "Alice" |
86 | | -} |
| 92 | +```bash |
| 93 | +sam delete |
87 | 94 | ``` |
0 commit comments