|
| 1 | +# Webhook Receiver with AWS Lambda durable functions - NodeJS |
| 2 | + |
| 3 | +This serverless pattern demonstrates a serverless webhook receiver using AWS Lambda durable functions with NodeJS. The pattern receives webhook events via API Gateway, processes them durably with automatic checkpointing, and provides status query capabilities. |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: https://serverlessland.com/testing/patterns/lambda-durable-webhook-sam-nodejs |
| 6 | + |
| 7 | +To Learn more about Lambda durable functions: |
| 8 | +- [AWS Lambda durable functions Documentation](https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html) |
| 9 | +- [Lambda durable functions Best Practices](https://docs.aws.amazon.com/lambda/latest/dg/durable-functions-best-practices.html) |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +* [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. |
| 16 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 17 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 18 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 19 | + |
| 20 | +## Architecture |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +## How It Works |
| 25 | + |
| 26 | +This pattern demonstrates a serverless webhook receiver using AWS Lambda durable functions. The pattern receives webhook events via API Gateway, processes them durably with automatic checkpointing, and provides status query capabilities. The durable function processes webhooks in 3 checkpointed steps: |
| 27 | + |
| 28 | +1. **Validate** - Verify webhook payload and structure |
| 29 | +2. **Process** - Execute business logic on webhook data |
| 30 | +3. **Finalize** - Complete processing and update final status |
| 31 | + |
| 32 | +This pattern acheives the following key features: |
| 33 | + |
| 34 | +- **Automatic Checkpointing** - Each processing step is checkpointed automatically |
| 35 | +- **Failure Recovery** - Resumes from last checkpoint on failure |
| 36 | +- **Asynchronous Processing** - Immediate 202 response, processing in background |
| 37 | +- **State Persistence** - Execution state stored in DynamoDB with TTL |
| 38 | +- **Status Query API** - Real-time status tracking via REST API |
| 39 | + |
| 40 | +**Note:** Each step writes status updates to DynamoDB before its main work. These writes are idempotent, so retries are safe. During replay, the DynamoDB status reflects the last successfully written state—not the current replay position. Status queries should treat intermediate states as "in progress." |
| 41 | + |
| 42 | +**Important:** Please check the [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/durable-functions.html) for regions currently supported by AWS Lambda durable functions. |
| 43 | + |
| 44 | +## Deployment |
| 45 | + |
| 46 | +1. **Build the application**: |
| 47 | + ```bash |
| 48 | + sam build |
| 49 | + ``` |
| 50 | + |
| 51 | +2. **Deploy to AWS**: |
| 52 | +Plese enter required `WebhookSecret` |
| 53 | + |
| 54 | +```bash |
| 55 | + sam deploy --guided |
| 56 | + ``` |
| 57 | + |
| 58 | + Note the outputs after deployment: |
| 59 | + - `WebhookApiUrl`: Use this for sending webhook POST requests |
| 60 | + - `StatusQueryApiUrl`: Use this for querying execution status |
| 61 | + |
| 62 | +## Testing |
| 63 | +To test the set-up, utilize the below curl command by replacing the WebhookApiUrl copied from the above step: |
| 64 | + |
| 65 | + ```bash |
| 66 | + # Send a test webhook |
| 67 | + curl -X POST <WebhookApiUrl> \ |
| 68 | + -H "Content-Type: application/json" \ |
| 69 | + -d '{ |
| 70 | + "type": "order", |
| 71 | + "orderId": "123456", |
| 72 | + "data": {"amount": 100} |
| 73 | + }' |
| 74 | + ``` |
| 75 | + |
| 76 | +Once the Webhook is submitted, to query the status of webhook, use the following curl command by replacing the StatusQueryApiUrl: |
| 77 | + |
| 78 | + ```bash |
| 79 | + # Get execution status (use executionToken from webhook response) |
| 80 | + curl <StatusQueryApiUrl> |
| 81 | + ``` |
| 82 | + |
| 83 | + **Success indicators:** |
| 84 | + - Webhook returns 202 with `executionToken` |
| 85 | + - Status query shows progression: `STARTED` → `VALIDATING` → `PROCESSING` → `COMPLETED` |
| 86 | + - Execution state persists in DynamoDB with TTL |
| 87 | + |
| 88 | +To simulate a validation failure, send an invalid payload (empty or missing required fields): |
| 89 | + |
| 90 | + ```bash |
| 91 | + # Send an invalid webhook (empty payload triggers validation failure) |
| 92 | + curl -X POST <WebhookApiUrl> \ |
| 93 | + -H "Content-Type: application/json" \ |
| 94 | + -d '{}' |
| 95 | + ``` |
| 96 | + |
| 97 | +Query the status to see the failure: |
| 98 | + |
| 99 | + ```bash |
| 100 | + curl <StatusQueryApiUrl> |
| 101 | + ``` |
| 102 | + |
| 103 | + **Failure indicators:** |
| 104 | + - Status query shows `FAILED` status |
| 105 | + - Error message indicates validation failure reason |
| 106 | + - Execution state persists in DynamoDB for debugging |
| 107 | + |
| 108 | +## Cleanup |
| 109 | + |
| 110 | +```bash |
| 111 | +sam delete |
| 112 | +``` |
| 113 | +---- |
| 114 | +Copyright 2026 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 115 | + |
| 116 | +SPDX-License-Identifier: MIT-0 |
0 commit comments