Setup Monitoring For SQS Using Cloudwatch
In the previous articles , I have explained , How you can
Monitor and auto-recover repaired EC2 instances
Monitor AWS resources using Cloudwatch Alarms
In this guide , We will learn How we can monitor the SQS queues using Cloudwatch.
By default , SQS sends metrics to the Cloudwatch Metrics.
Using the cloudwatch metrics , We can monitor the SQS Queues for parameters such as MessagesAvailable , Oldest messages , Not Visible messages , Number of messages sent , Number of Messages received , Number of messages deleted etc.
Monitoring SQS Queues
As said earlier , We are going to setup monitoring for
- Visible Messages
- Oldest Messages
- Number of messages received
- Number of messages sent
- Number of messages deleted
To create cloudwatch alarms , Login to Cloudwatch console.
In the left navigation pane , Choose Metrics
From the lists of available metrics , Choose SQS
And then select Queue Metrics
There we can find the lists of SQS queues and their respective Metric names such as NumberofMessagesSent , NumberofMessagesReceived , ApproximateAgeofOldestMessages etc.
Lets say you want to check the metrics of a Queue for the NumberOfMessagesReceived for a particular time period.
A graph will be plotted for the same.
We can adjust the time duration for the metrics.
Lets use this metrics and setup the monitoring for the SQS Queues.
Services Used For Monitoring And Alerting
- Cloudwatch Alarms
- SNS Topic with Subscriber
Creating Cloudwatch Alarms
Lets go ahead and create a Cloudwatch alarm for
ApproximateNumberOfMessagesVisible
Login to Cloudwatch console , In the left pane , Choose Alarms
Then Click Create alarm
Under Specify metric and conditions , Click Select metric
From the lists of available metrics , Choose SQS and then click Queue Metrics
Select the QueueName and the Metric Name ApproximateNumberOfMessagesVisible
and then click Select metric
Under Statistic Choose Minimum and Under Period Choose 1 minute
and Under Conditions
Threshold type should be static and whenever ApproximateNumberOfMessagesVisible is
Greater/Equal to 100
When the queue is holding more than 100 messages in the period of 1 minute we will be alerted.
Click Next , Here we are going to setup alerting for two cases.
- If the metric value goes above the threshold (In alarm)
- If the metric value goes below the threshold (OK)
Here we have to either use an existing SNS topic or We can create a new topic.
If you want to setup a SNS topic with a subscribers , Refer here
Once the SNS setup is completed , You can resume with the Notifications.
Or if you want to create SNS topic from cloudwatch console , Select Create new topic
Provide a name for the SNS topic and add the email addresses (who should receive the notifications) and then click Create topic
Once it is created , You should see as shown below.
We have now added for In alarm notification.
To get notifications for OK status , Choose Add notification and then select OK and select existing topic and we already have created a topic with a subscriber.
and then click Next
Provide a name for the alarm , For better identification , provide a name as follows,
Environment-SQSname-Metricname
You can also add a short description for each alarm you create.
Click Next and then Choose Create Alarm.
Now we have created an alarm so whenever the queue holds messages more than 100 we will be notified.
ApproximateAgeOfOldestMessage
Lets create a alarm for Oldest messages available in the queue.
We are going to following the same procedure , The only place that need modification is Specify metric and conditions
Here we are going to choose ApproximateAgeOfOldestMessage
Under Conditions , For static threshold , Lets say Whenever a new messages comes to the queue It should not take more than 2 mins (120 sec) to process that particular message , If it is not processed within 2 mins we should be notified.
Click Next and Setup notifications for In alarm and OK state as discussed above.
You can use the same SNS topic.
Provide a name for the alarm and then click Create alarm
ApproximateNumberOfMessagesDelayed
The number of messages in the queue that are delayed and not available for reading immediately.
This will happen when the queue is configured as a delay queue or when a message has been sent with a delay parameter.
Lets create a alarm for Delayed messages in the queue.
We are going to following the same procedure , The only place that need modification is Specify metric and conditions
Here we are going to choose ApproximateNumberOfMessagesDelayed
Under Conditions , For static threshold , When the delayed messages in more than 100 in a Queue.
Click Next and Setup notifications for In alarm and OK state as discussed above.
You can use the same SNS topic or you can create a new one.
Provide a name for the alarm and then click Create alarm.
NumberOfMessagesSent
If more then 50 messages are sent to a queue in 15 mins , Send out a notification.
Lets create a alarm for NumberOfMessagesSent to a queue.
We are going to following the same procedure , The only place that need modification is Specify metric and conditions
Here we are going to choose NumberOfMessagesSent
Under Conditions , For static threshold , When the queue has more than 50 messages for the period of 15 minutes
Click Next and Setup notifications for In alarm and OK state as discussed above.
You can use the same SNS topic.
Provide a name for the alarm and then click Create alarm
You can repeat the same process for all other metrics available in cloudwatch for the SQS.
Conclusion
We have successfully implemented a monitoring and alerting mechanism using Cloudwatch alarm and SNS topic for SQS Queues
Hope you find it helpful. Thanks for reading this article.
Please check my other articles.