Node.js
This is a step by step guide on how to receive webhooks from QStash in your Lambda function on AWS.
1. Create a new project
Let’s create a new folder called aws-lambda
and initialize a new project with
your favourite package manager. This example uses npm
because everyone will
have it installed with node already.
2. Install Dependencies
Using typescript requires installing a few dependencies. Here we are using
esbuild
to bundle our code, so we can upload it to AWS later.
3. Creating the handler function
In this example we will show how to receive a webhook from QStash and verify the signature without any additional dependencies.
First, let’s import everything we need:
As you can see, we only need the type definitions from AWS and some crypto functions from the node standard library.
Next we create the handler function. Ignore the verify
function for now. We
will add that next. In the handler we will prepare all necessary variables that
we need for verification. This includes the signature, the signing keys and the
url of the lambda function. Then we try to verify the request using the current
signing key and if that fails we will try the next one. If the signature could
be verified, we can start processing the request.
The verify
function will handle the actual verification of the signature. The
signature itself is actually a JWT and includes claims about
the request. See here.
You can find the complete file here.
That’s it, now we can create the function on AWS and test it.
4. Create a Lambda function on AWS
Create a new Lambda function from scratch by going to the AWS console. (Make sure you select your desired region)
Give it a name and select Node.js 16.x
as runtime, then create the function.
Afterwards we will add a public URL to this lambda by going to the
Configuration
tab:
Select Auth Type = NONE
because we are handling authentication ourselves.
After creating the url, you should see it on the right side of the overview of your function:
5. Set Environment Variables
Get your current and next signing key from the Upstash Console
On the same Configuration
tab from earlier, we will now set the required
environment variables:
6. Deploy your Lambda function
We need to bundle our code and zip it to deploy it to AWS.
Add the following script to your package.json
file:
When calling npm run build
this will build and zip the code.
Afterwards we can click the Upload from
button in the lower right corner and
deploy the code to AWS. Select ./dist/index.zip
as upload file.
7. Publish a message
Open a different terminal and publish a message to QStash. Note the destination url is the URL from step 4.
Next Steps
That’s it, you have successfully created a secure AWS lambda function, that receives and verifies incoming webhooks from qstash.
Learn more about publishing a message to qstash here
QStash SDK
Our Typescript Sdk will handle verification automatically if you want to use it.
Was this page helpful?