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 common example of a message queue is E-mail 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 ready to read it.
In this guide, we will 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 an event occurs in Contentstack, a webhook triggers the Lambda function and puts the data into the AWS SQS queue. This data is then processed to perform some automated tasks and the data (message) dequeues automatically after processing.
To proceed with the procedure, you will need the following:
Follow the steps given below to set up webhooks with AWS SQS:
Create a queue in AWS to store the messages (incoming data from webhooks).
Note: Make sure to copy and store the queue URL displayed in the Details section, as we will be using it in the following steps.
Create a lambda function that sends a message (received from Contentstack Webhooks) to the SQS queue.
Note: Make sure to add AmazonSQSFullAccess and AWSLambdaSQSQueueExecutionRole policies by selecting the IAM service from the Services list and then selecting Roles > Attach Policies. In the window that pops up, search for the above two policies and add them.
'use strict'; var AWS = require('aws-sdk'); var sqs = new AWS.SQS({ region: '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); }); }
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:
After performing the above steps, you'll get an API URL that you can use while configuring webhooks in the next step.
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.
Create another AWS lambda function to poll the messages from the queue after the messages are added in the queue.
'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); };
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. To do this, follow the steps given below:
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