Building a Webhook in AWS
Tags: webdev, aws, serverless, devops
Published: 2023-06-26 21:33:47

## 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](https://www.redhat.com/en/topics/automation/what-is-a-webhook)

“_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](https://aws.amazon.com/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q3i5af8iicr068ty2rw8.png)


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

![Basic information](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbpit0k33tqodt662dfe.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iepnzn361e663y87jf28.png)

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;
};

```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y16zqsmjh70wgkyewi7s.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eb1aqm0zl5nholrnmyck.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ga3548nhpql2vnwl1b8v.png)

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

![Postman confirmation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ixjxav00x17o0l1s7yy5.png)

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!!