Building WebHook is easy — using AWS Lambda and API Gateway

Yashish Dua
MindOrks
Published in
5 min readApr 26, 2019

Webhooks are an integral part of any company’s infrastructure. They can do a varied types of work and help in improving communication between internal and external services. If I say that you don't need a server anymore to host your webhooks, building them is super easy and they can work independently from your services, would you believe me? You have to! With AWS Serverless Platform you can build a Webhook which runs independently and you don’t have to worry to monitor and manage it. Be with me, I will explain how to create a webhook using AWS Lambda and API Gateway in simple steps. Trust me, it’s too simple!

Note: I am assuming you have AWS account set up. And know about Avengers.

What is Lambda?

AWS Lambda lets you run your code without creating or managing any servers. Moreover, you only for the compute time. They don’t charge you when your code is not running. Read more here.

Or you can assume Lambda as Thor’s hammer ~ Mjolnir

What is API Gateway?

It is an API proxy which is fully managed service. It helps you create APIs and publish, monitor and maintain them. It is scalable, can handle concurrent calls, authorization, traffic management, and a lot of other things. Read more here.

Or you can assume API Gateway as the great Asgard king Thor himself.

Building Lambda

There is a special cooked recipe to build the THOR’s hammer. Head to Asgard to know more.

Step 1: Head to AWS Console, select Services and search for Lambda

Step 2: Select Create Function and then Author from Scratch option.

Step 3: Configure your Lambda.
Put your almighty best element to the hammer! Don’t miss taking help if needed.

I have chosen Node.js as my run time language, chose what you love! Execution role needs to be changed if your organization use IAM roles and you want rules over Lambda else to keep it what I have done.

Important Points

You can add triggers to your lambda. For example, you might want to log and send the logs to AWS Cloudwatch or you might want to push an event to SNS if your lambda is invoked. You can read about all the integrations on AWS.

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.
exports.myHandler = function(event, context, callback) {
... function code
callback(null, "some success message"); //
or // callback("some error type"); }

Since I am not writing this article to explain how Lambda works rather how you can Lambda to build a webhook, it’s better I leave learning Lambda to you. So, once you are done building the lambda which does something on an event (Maybe calls your internal API with some data or post a message to Slack), it’s time to make this lambda accessible via an API.

Make sure you add the power force from the heart of the star to this hammer.

Setting up API Gateway

Seems like Thor is not ready to pick up the hammer. We have to teach him.

Step 1: Head to AWS Console, select Services and search for API Gateway

Step 2: Create API and the following dashboard will appear.

For a webhook, we would be interested in the REST protocol. An important thing to note here is to select an Endpoint Type which is by default Regional which means the lambda would be deployed in the current region (When you created your AWS account you must have selected a region, this is exactly that!). AWS provides more interesting endpoint type like Edge optimized in which your Lambda is deployed and distributed over multiple regions and its cache is also maintained. AWS CloudFront services handle all these things inside Edge.

Step 3: 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 if you want to send data or POST data to this lambda from somewhere.
After that, you have to set up the method you just created.

Make sure you check LAMBDA PROXY INTEGRATION. It’s necessary to build a webhook. This API Gateway will provide you an endpoint. We need to redirect it to Lambda.

Step 4: 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 else copy mine.

Hit deploy And here you go! Grab your invoke URL in the stage area. There are more setting you can do like setting the timeout, firewall, throttling, etc.

Now, we have taught Thor to use the hammer and make use of its maximum power! We are in the endgame.

--

--