|
| 1 | +# Lambda Durable Function - REST API Call with Node.js |
| 2 | + |
| 3 | +This pattern demonstrates a Lambda durable function that calls an external REST API using Node.js. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: << Add the live URL here >> |
| 6 | + |
| 7 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + ``` |
| 20 | + git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ``` |
| 24 | + cd lambda-durable-rest-api-sam-js |
| 25 | + ``` |
| 26 | +
|
| 27 | +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 28 | + ``` |
| 29 | + sam build |
| 30 | + sam deploy --guided |
| 31 | + ``` |
| 32 | +1. During the prompts: |
| 33 | + * Enter a stack name |
| 34 | + * Enter the desired AWS Region |
| 35 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 36 | +
|
| 37 | + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 38 | +
|
| 39 | +1. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 40 | +
|
| 41 | +## How it works |
| 42 | +
|
| 43 | +This pattern demonstrates AWS Lambda Durable Execution for calling external REST APIs. It uses the AWS Durable Execution SDK to create a durable function that can: |
| 44 | +
|
| 45 | +**AWS Durable Execution Features:** |
| 46 | +- **Automatic State Management**: AWS manages execution state across invocations |
| 47 | +- **Durable Steps**: The `context.step()` method marks functions that can be retried automatically |
| 48 | +- **Durable Waits**: Use `context.wait()` to pause execution without consuming CPU or memory |
| 49 | +- **Built-in Retry Logic**: Failed steps are automatically retried by AWS |
| 50 | +- **Execution History**: AWS tracks the complete execution history and state |
| 51 | +
|
| 52 | +The function uses the `withDurableExecution` wrapper to mark the Lambda handler as a durable execution workflow. All steps defined with `context.step()` are automatically retryable. |
| 53 | +
|
| 54 | +AWS Lambda Durable Execution automatically handles failures, retries, and state persistence without requiring external services like DynamoDB or Step Functions. |
| 55 | +
|
| 56 | +**Note**: This pattern requires Node.js 24.x runtime which has native support for durable execution. |
| 57 | +
|
| 58 | +## Testing |
| 59 | +
|
| 60 | +1. Get the function name from the stack outputs: |
| 61 | +```bash |
| 62 | +aws cloudformation describe-stacks --stack-name <your-stack-name> \ |
| 63 | + --query 'Stacks[0].Outputs[?OutputKey==`FunctionName`].OutputValue' --output text |
| 64 | +``` |
| 65 | + |
| 66 | +2. Invoke the function with default URL: |
| 67 | +```bash |
| 68 | +aws lambda invoke \ |
| 69 | + --function-name <function-name> \ |
| 70 | + --payload '{}' \ |
| 71 | + response.json && cat response.json |
| 72 | +``` |
| 73 | + |
| 74 | +3. Invoke with a custom URL: |
| 75 | +```bash |
| 76 | +aws lambda invoke \ |
| 77 | + --function-name <function-name> \ |
| 78 | + --payload '{"url": "https://jsonplaceholder.typicode.com/users/1"}' \ |
| 79 | + response.json && cat response.json |
| 80 | +``` |
| 81 | + |
| 82 | +Example JSON Lambda test event: |
| 83 | +```json |
| 84 | +{ |
| 85 | + "url": "https://jsonplaceholder.typicode.com/posts/1" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Expected response (success): |
| 90 | +```json |
| 91 | +{ |
| 92 | + "statusCode": 200, |
| 93 | + "headers": {"Content-Type": "application/json"}, |
| 94 | + "body": "{\"message\": \"API call successful\", \"url\": \"https://jsonplaceholder.typicode.com/posts/1\", \"data\": {...}}" |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +The execution is durable - if the API call fails, AWS Lambda will automatically retry the `callRestApi` step without re-executing the entire function. |
| 99 | + |
| 100 | +## Cleanup |
| 101 | + |
| 102 | +1. Delete the stack: |
| 103 | + ```bash |
| 104 | + aws cloudformation delete-stack --stack-name <your-stack-name> |
| 105 | + ``` |
| 106 | +1. Confirm the stack has been deleted: |
| 107 | + ```bash |
| 108 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'<your-stack-name>')].StackStatus" |
| 109 | + ``` |
| 110 | + |
| 111 | +---- |
0 commit comments