Was this article helpful?
Thanks for your feedback
A message queue is a middleware solution that allows systems and applications to communicate with each other by storing data temporarily in a node before it's available to use by the other apps. A typical example of a message queue is Email, where a sender sends a message; it gets stored in an intermediate node until the system or the app is ready to advance it. At the receiving end, the message is stored in an email box until the receiver is prepared to read it.
In this guide, you'll learn how to implement a message queuing system using Contentstack Webhooks, AWS SQS (a message queue service from Amazon), and AWS Lambda to automate a series of actions (e.g., notifications, file sharing, etc.).
Overview of the process: When you publish/unpublish any entry or asset in Contentstack, a webhook will trigger. This webhook will invoke the AWS Lambda function which will add the data to the SQS queue. You can then use this data as per your requirement to perform automated tasks, after which the data dequeues automatically.
Here are the steps to automate the creation of an entry in Contentstack:
To proceed with the procedure, you will need the following:
Create a queue in AWS to store the messages (incoming data from webhooks) by performing the following steps:
Now let’s create a lambda function that sends a message (received from Contentstack Webhooks) to the SQS queue by performing the following steps
Click on the Create function button. This will create your lambda function and take you to the lambda function’s page.
'use strict'; var AWS = require('aws-sdk'); var sqs = new AWS.SQS({ region: '<your_region_code>' //for example, us-east-1 }); exports.handler = function(event, context, callback) { //var accountId = context.invokedFunctionArn.split(":")[4]; var queueUrl = '//paste your queue url here'; // response and status of HTTP endpoint var responseBody = { message: '//text' }; var responseCode = 200; // SQS message parameters var params = { MessageBody: event.body, QueueUrl: queueUrl }; sqs.sendMessage(params, function(err, data) { if (err) { console.log('error:', "failed to send message" + err); var responseCode = 500; } else { console.log('data:', data); responseBody.message = 'Sent to ' + queueUrl; responseBody.messageId = data.MessageId; } var response = { statusCode: responseCode, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(responseBody) }; callback(null, response); }); }
You need to attach certain policies to the role you created in the above step. To do so, perform the following steps:
For your Lambda to be accessible publicly, you need to configure the AWS API Gateway using your function as the backend for the API as shown below:
To create and set up a webhook log in to your Contentstack account and perform the following steps:
With this, we have set up the webhooks for triggering messages.
Next, let’s create another AWS lambda function to poll the messages from the queue after the messages are added to the queue by performing the following steps:
'use strict'; exports.handler = (event, context, callback) => { const response = { statusCode: 200, body: JSON.stringify({ message: 'Received message successfully!', input: event, }), }; var body = event.Records[0].body; console.log("Received message: ",body,event.Records[0]); callback(null, response); };Next, let’s create an SQS trigger that will activate when a message is enqueued. This trigger will process the message and delete it from the queue after its successful processing.
Note: This will only log the data in the AWS Cloudwatch. You can write your business logic to process the received messages.
After completion of this setup, go to Contentstack, publish any entry (from which Webhook will trigger), and then check the logs in AWS Cloudwatch.
Additional Resource: To use webhooks for managing translation, you can refer to our guide on Setting up Translation System with Contentstack Webhooks, Memsource, and AWS Lambda for more details.
Now that you know how to use AWS SQS, you can try to set up a notification System with Contentstack Webhooks, AWS, Lambda, and AWS SNS.
Was this article helpful?
Thanks for your feedback