How to Receive and Replay External Webhooks in AWS Lambda with Hookdeck
Webhooks play a crucial role in facilitating seamless, real-time communication between different services in modern application development. Imagine being able to host and manage webhooks without the hassle of maintaining servers. The AWS Serverless Platform, specifically AWS Lambda and API Gateway, allows you to achieve this easily.
Hookdeck is a webhook management platform that helps developers reliably receive webhooks, manage events, and troubleshoot issues quickly. By integrating Hookdeck with AWS Lambda and API Gateway, you can enhance the management and reliability of your webhook events, ensuring seamless integration with external systems.
This guide walks you through how to create webhook endpoints using AWS Lambda and API Gateway, and then how to receive and replay a webhook using Hookdeck.
Understanding AWS Lambda and API Gateway
Before we dive into creating webhook endpoints, let’s go over what AWS Lambda and API Gateway are, respectively.
AWS Lambda: AWS Lambda allows you to execute code without having to provision or manage servers. You only pay for the compute time you consume, and AWS takes care of scaling and infrastructure management.
API Gateway: API Gateway is a fully managed service that enables you to create, publish, maintain, monitor, and secure APIs at any scale. It acts as a powerful API proxy and can handle tasks such as authorization and traffic management.
Create webhook endpoints with AWS Lambda and API Gateway
Now let’s go through the steps required to create webhook endpoints using AWS Lambda and API Gateway.
Step 1: Create an AWS Lambda function
Head to the AWS Console, search for Lambda in the list of services, and open it.
Click on Create function and choose the Author from scratch option.
Configure your Lambda function and click on Create function.
- Set the function name (e.g.
webhookHandler
) - Choose the runtime language (e.g. Node.js)
- Customize execution role if needed
- Set the function name (e.g.
The function gets created and you are presented with an inline code editor where you can edit the existing code or upload a ZIP file with your code and dependencies.
- Modify the Lambda function code with this basic example and Deploy:
export const handler = async (event) => {
let response;
try {
if (event.httpMethod !== "POST") {
throw new Error("Only POST method is allowed");
}
// Get POST body
const body = JSON.parse(event.body);
// Implement business logic with body data
response = {
statusCode: 200,
body: JSON.stringify({ message: "Webhook received!" }),
};
} catch (error) {
// Print debug error message
console.error(error);
response = {
statusCode: 400,
body: JSON.stringify({ message: error.message }),
};
}
return response;
};
Step 2: Set up an API Gateway
An API Gateway can be added to the function quickly by following the steps below.
- In the AWS Console, search for API Gateway from the list of services.
- Click on Create API. Choose the REST API option and the REST protocol in the protocol type.
- Configure the new API and set the API name and Endpoint Type to “Regional”, then Create API.
Create a method for your Lambda function:
- Click on Actions and select Create Method
- Choose the appropriate HTTP method (e.g.
POST
) based on your webhook’s functionality.
Configure the method:
- Check the “Lambda Function” on the Integration type
- Check Use Lambda Proxy integration. This is crucial for building a webhook.
- Set the Lambda Function to the function created above,
webhookHandler
, and Save.
Deploy the API to a stage:
- Click on Actions and select Deploy API
- Create a new stage or select an existing one and Deploy.
After deployment, you get an Invoke URL alert in the stage area. Note this URL as it is needed below for the webhook invocation.
On your function details page, you see the API Gateway automatically set as a trigger to the
webhookHandler
function.
Learn more about AWS Lambda here.
Integrate Hookdeck for webhook replay
By using Hookdeck as a gateway between your source platform's webhooks and Lambda Functions, you can reliably receive webhooks and replay any failed one accordingly.
Here is how to achieve that.
Create a Hookdeck connection
Go to your Hookdeck Dashboard and create a new Connection.
- Source: The external platform where you want to receive webhooks from.
- Destination: The HTTP URL of the Gateway API created above.
After creating the connection, you are given a URL to use on the source platform.
The created connection allows webhook events from your source to be relayed directly to your Lambda function through Hookdeck.
Receive webhook events
You can utilize the Hookdeck Console to simulate example webhooks from popular webhook sources.
Imagine your webhook source is Stripe. When an event is triggered, you can monitor it on both the Hookdeck dashboard and in the logs of the Lambda function.
From you Hookdeck dashboard, go to the Events tab in the side panel. Filter the events based on the connection you created earlier. This displays the activities of events on that connection.
On the details page of the Lambda function, navigate to Monitor > Logs to see the log about the events.
Replay error or failed webhooks
In case any failed events occur and the Lambda function doesn’t execute properly, Hookdeck makes it possible for such an event to still be replayed from the Hookdeck event panel.
Click on the kebab menu across the failed event and Retry. This attempts to send the event to your destination again.
By combining AWS Lambda, API Gateway, and Hookdeck, you’ve learned a straightforward way to create, manage, and enhance the reliability of webhook endpoints. This allows you to build integrations without managing servers. Follow this guide to get started building event-driven workflows with webhooks on AWS.
Take a look at Hookdeck's documentation to explore more.