Notification System with Contentstack Webhook, AWS, Lambda, and AWS SNS

Contentstack provides webhooks that trigger whenever certain events occur in different modules of your stack. You can invoke a lambda function using the webhooks. This means that you can run JavaScript code whenever something happens in Contentstack. This integration can help you automate a series of actions.

In this guide, we will learn how to invoke a Lambda function (when something happens in Contentstack) that sends a message to an AWS SNS topic. This AWS SNS topic can then send a notification, along with the payload, to any app, service or endpoint of your choice.

Pre-requisites

To proceed with the procedure, the following is required:

  • Basic knowledge of AWS SNS
  • Working knowledge of AWS API Gateways
  • Access to the AWS environment
  • Contentstack account

Set up Lambda Webhook Listener

Setting up an AWS Lambda Webhook Listener is a 4-step procedure:

  1. Create an AWS Lambda function
  2. Configure AWS API Gateways
  3. Deploy your APIs
  4. Configure a webhook in Contentstack

Step 1. Create an AWS Lambda Function

Create an AWS Lambda function that sends the data (received from Contentstack via webhooks) to AWS SNS.

  1. Login to your AWS Management Console, select AWS Lambda Service from the service list.
  2. Click on the Create Function button, and then the Author from Scratch option.
  3. Configure the lambda based on your requirements. Choose Node.js as your run-time language.

    Note
    : You can use any language instead of Node.js. You can also add triggers to your lambda to push an event to SNS when the lambda is invoked.

  4. AWS Lambda offers an inline code editor. You can write your lambda code here. Another option is to write the lambda code on your machine and then load it in the code editor. The inline editor has the basic code written, you can replace it with your own code as shown  in the example below:

    AWS.png
  5. In the Lambda function handler and role, keep the index.handler as Handler and for the Role choose an existing role or create a new role from template(s).
  6. Review your setting and click on the Create Function option if everything you entered is correct.

Now that you have configured your lambda, let's move on and configure the AWS API Gateways.

Step 2. Set up API Gateway

For your Lambda to be accessible publicly through an HTTP POST, you need to configure the AWS API Gateway using your function as the backend for the API as shown below:

  1. Log in to AWS Management Console and select API Gateways from the services list.
  2. Click on the Getting started or Create API button (depending on whether you have already an API configured or not).
  3. From the available options, select New API. Enter the name and description of your API.
  4. Click on Actions in the Resources column, and select Create Method.
  5. From the resultant dropdown, Select POST and click on the check mark.
  6. Select Lambda Function as the integration type for this endpoint.
  7. Select the Lambda Region, and then type in the name of your Lambda Function.

    Note: The region into which your Lambda is deployed isn’t easily identifiable when you’re deploying your Lambda, so you might have to do a little hunting.

    AWS Lambda Function.png
  8. Click on the Save button. You’ll get a notification which asks you to confirm that this API will be granted permission to invoke your Lambda function. 
  9. Click OK to grant the permission.

Step 3. Deploy Your API

To make your API ready to use, you need to deploy your API.

  1. From the Actions drop-down. select Deploy API.
  2. Select [New Stage] in the Deployment stage and enter prod (or anything you like to identify this stage) in the Stage name.
  3. Click on the Deploy button.

Your API is now deployed and you will get a URL, which you can use to invoke the APIs.

Step 4. Configure a Webhook in Contentstack

Now to trigger the event on any update on the entry, you will have to set up a Webhook in Contentstack.

  1. Log in to your Contentstack account and click on Settings.
  2. Select Webhooks and then click on + New Webhook
  3. Enter the name for your Webhook, for example, AWS-Webhook Listener.
  4. In the URL to notify field, enter the URL to be notified when the event is triggered. This URL is the one which you generated when you deployed your APIs (as shown in the above section). Complete the remaining settings as shown in the screenshot below:

    CS Webhook for Lambda Function.png
  5. When any content is updated, the condition specified inside the 'When' section gets executed which triggers the notification.
  6. Click on the Save button to save your webhook settings.
  7. Now try updating an entry and you should receive a notification at the mentioned URL.

Note: To learn how to work with Webhooks, refer to our doc site at here:

    Was this article helpful?
    top-arrow