DEV Community

Cover image for Building a Webhook in AWS
thesonicstar
thesonicstar

Posted on • Updated on

Building a Webhook in AWS

How to handle real-time updates from 3rd parties via webhooks with highly scalable serverless AWS Lambda functions behind API Gateway.

First lets provide a brief definition of what a webhook and what AWS Lambda is:

A webhook is an HTTP-based callback function that allows lightweight, event-driven communication between 2 application programming interfaces (APIs).” - RedHat

AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. You can trigger Lambda from over 200 AWS services and software as a service (SaaS) applications, and only pay for what you use.” - AWS Lambda

Now lets get started.
Login to the AWS Console.

First we will build a Lambda Function. In the search bar type in “Lambda”
Select “Create Function” then “Author From Scratch”

Create function

Fill out the basic information:
Function name: You decide
Runtime: You decide (Latest version) – chosen NodeJs 18.x for my demo.

Basic information

Once complete, click on “Create Function” at the bottom of the page.

AWS Lambda provides an inline code editor. You can either write Lambda’s code on your local, create a zip alongside it’s installed node modules and then upload it here or write down the code here. But if you need any external package to be imported and installed, it cannot be done here because you cannot run npm install here.

You will find a basic handler code written which catches your event.

Lambda Function

You can write your code here. This will invoke when the Lambda is instigated. For now I will leave the demo code alone in place. (As this is just a simple walkthrough on how to build a webhook).
Code is below:

export const handler = async(event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Enter fullscreen mode Exit fullscreen mode

And that’s our Lambda function created!

API Gateway

Next step is to create the API gateway that will trigger this function.
In the search bar type in “API gateway”
Choose “Rest API” as the API type
Then under Create new API select “New API”

New REST API

Creating a method for our Lambda
Go to Actions and select Create method option. It depends on your webhook functioning and use-case which method to pick. Use PUT or POST if you want to send data to your lambda function from somewhere.
After that, you have to set up the method you just created. Click “Save”

Method creation

Deploying API to a stage
Go to Actions and click Deploy API. If you have created a stage, select existing one else it will ask you to create a new one. Add your own stage information.

Hit deploy and here you go! Grab your invoke URL in the stage area.
The new invoke URL will be presented to you on the next page. Take a note of this for your application.

Invoke URL

We can test this all out, using a software like POSTMAN, SOAPUI etc. I performed quick test using POSTMAN.

Postman confirmation

You can see a status code is returned as well as the message configured in the Lambda function.

Well done. You have created your first webhook.

Go on and spread the knowledge. Share this article to friends and family!!

Top comments (0)