Author picture Fongang Rodrique

How to Receive and Replay External Webhooks in AWS Lambda with Hookdeck

Published · Updated


Webhooks play a crucial role in facilitating seamless, real-time communication between different services in modern application development. The AWS Serverless Platform, specifically AWS Lambda and API Gateway, allows you to host and manage webhooks without the hassle of maintaining servers.

Hookdeck is a reliable event gateway for managing inbound events, including monitoring and replaying webhooks, and can be integrated with AWS Lambda and API Gateway, to enhance the management and reliability of your webhook events, ensuring seamless integration with external systems.

This guide covers a step-by-step approach to creating a webhook endpoint using AWS Lambda and API Gateway, and then receiving and replaying 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 provisioning or managing servers. You only pay for the compute time you consume, and AWS handles 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 is a powerful API gateway that handles authorization and traffic management tasks.

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

    Create an AWS Lambda function

The function is 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 below and click on Deploy
export const handler = async (event) => {
  let response;

  try {
    if (event.httpMethod !== "POST") {
      throw new Error("Only POST method is allowed");
    }

    const body = JSON.parse(event.body);

    // Implement any logic with the body data

    response = {
      statusCode: 200,
      body: JSON.stringify({ message: "Webhook received!" }),
    };
  } catch (error) {
    // Catch and print any debug error message
    console.error(error);

    response = {
      statusCode: 400,
      body: JSON.stringify({ message: error.message }),
    };
  }

  return response;
};

Deploy Lambda function

Step 2: Set up an API Gateway

Add an API Gateway to the function by following the steps below.

  • In the AWS Console, search for API Gateway from the list of services.
  • Click on Create API.
  • Click Build accross the REST API option as the API type.
  • Configure the new API and set the API name and the Endpoint Type to “Regional”, then Create API.

Set up an API gateway

  • Create a method for your Lambda function:

    • Click to open the newly created API gateway and hit on Create method
    • Choose the appropriate HTTP method (e.g., POST) based on your webhook handler functionality.
    • Check “Lambda Function” as the Integration type
    • Enable Lambda Proxy integration. This is crucial for building webhooks.
    • Set the Lambda Function to the function created above, **webhookHandler.
    • Click on Create method

    Set the Lambda function

  • Deploy the API to a stage:

    • Click on Deploy API. A pop-up is shown to specify the deployment stage.
    • 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.

    Invoke Lambda URL

On your function details page, you see the API Gateway automatically set as a trigger to the webhookHandler function.

Learn more about AWS Lambda in this AWS Lambda Documentation.

Integrate Hookdeck for webhook replay

Using Hookdeck as a gateway between your source platform's webhooks and Lambda Functions ensures reliable receipt and retry of failed webhooks.

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.

    Create a Lambda Hookdeck connection

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 your Hookdeck dashboard, go to the Events tab on the side panel. Filter the events based on the connection you created earlier, which displays the activity of events on that connection.

Lambda function in Hookdeck Events

On the details page of the Lambda function, switch to the Monitor tab to see the log about the events.

Replay error or failed webhooks

If any failed events occur and the Lambda function doesn't execute properly, Hookdeck makes it possible to retry and event from the Hookdeck events panel.

From the events tab, click on the Retry icon across the failed event. This attempts to send the event to your destination again.

Replay Lambda event

By integrating Hookdeck with AWS Lambda and API Gateway, you can ensure that your webhook events are received and processed reliably, even in the face of failures. Hookdeck's robust monitoring tools allow you to track webhook activity and diagnose issues in real-time. At the same time, Hookdeck's retry functionality ensures no event is lost, providing a safety net for critical data.

To explore more features and learn more about Hookdeck, check out the Hookdeck documentation.