How to create Cloudwatch alarms for an SQS Queue on AWS

CloudWatch metrics for SQS queues are collected and pushed to CloudWatch at one-minute intervals automatically. These metrics are provided at no charge in CloudWatch for both standard and FIFO queues. Following are the metrics available in Cloudwatch to view and analyze SQS queues.

  1. ApproximateAgeOfOldestMessage: This metric returns the approximate age of the oldest non deleted message in the queue.
  2. ApproximateNumberOfMessagesDelayed: This gives the number of messages in the queue that are not available for reading immediately and delayed. 
  3. ApproximateNumberOfMessagesNotVisible: It is the number of messages that are in flight, i.e. they have been sent to a client but not yet deleted or not yet reached to the client.
  4. ApproximateNumberOfMessagesVisible: It is the number of messages available in the queue for retrieval.
  5. NumberOfEmptyReceives: It is the count of ReceiveMessage API calls that did not return a message.
  6. NumberOfMessagesDeleted: This returns the number of messages deleted from the queue.
  7. NumberOfMessagesReceived: It is the number of messages returned by calls to the ReceiveMessage action.
  8. NumberOfMessagesSent: It is the number of messages added to a queue.
  9. SentMessageSize: This gives the size of messages added to a queue.

For more details on the available metrics, click here to visit the official documentation.

In this article, we will create an alarm for the "NumberOfMessagesSent" metric. Before we proceed, it is assumed that you have some basic understanding of SQS queues, SNS Topics. It is also assumed that you at least have 1 active SQS queue and an SNS topic with a subscription. 

Pre-requisites

  1. AWS Account(Create if you don’t have one).
  2. SQS Queue.
  3. SNS Topic(Click here to learn to create an SNS Topic using Cloudformation)

What will we do?

  1. Login to AWS.
  2. Create an alarm for the SQS queue.

Login to AWS

Click here to go to AWS Login Page.

You will see the login page as follows when you hit the above link. Enter your credentials to login into your AWS account.

Login screen

Once you log in successfully into your account, you will see the main AWS Management Console as follows. Here, you can choose the desired region.

AWS Management console

Create an alarm for the SQS queue.

Search for "Cloudwatch" in the search box at the top of the screen.

Search Cloudwatch

You will see the Cloudwatch dashboard as follows. This dashboard gives you an overview of Cloudwatch. You can give it a try and explore on your own as this is not in the scope of this article.

Click on "Alarms" in the left panel.

Cloudwatch Dashboard

If you have any existing alarms you will see them here. Make sure you are in the desired region.

To create a new alarm for an existing SQS Queue click on the "Create alarm" button. 

Alarms screen

You will get a screen to specify a metric and conditions for the alarm to be created. Click on the "Select metric" button.

Specify metric and conditions

You'll get a list of services with metrics available for them.

Select metric

In the search box, search for SQS and you'll get the filtered result as "SQS> Queue metrics". Click on the SQS result.

Browse SQS

On this screen, you will see available metrics for the queues you have in your account. Let's select any one of these to create an alarm for it. Here, let's proceed with the "NumberofMessagesSent" metric. Click on the "Select metric" button to proceed.

Select metric

Choose the Statistics, Threshold type, Condition, and Threshold value as per your choice. Here, the alarm will be triggered if the sum of the number of messages sent is greater than 5 for 5 minutes.

Specify metric and conditions

Choose the Alarm state Trigger, SNS Topic. When the alarm is triggered it will send a notification to the specified SNS topic.

Configure actions

Give name and description to alarm and click on the "Next" button.

Add name and description

Review the configuration and click on the "Create alarm" button which will create an alarm.

Preview and create

Now you can see the alarm we just created. The state of the alarm will change based on the threshold value and condition. 

Alarm in OK state

Conclusion

In this article, we created an alarm for the existing SQS queue. The alarm we created is triggered when the "Sum" of "NumberofMessagesSent" is "Greater" than the threshold value "5" for "5 minutes", and when it is triggered a notification is sent to the SNS topic. You can now follow the same steps to create alarms for any other available metric, with different statistics and the threshold value.

Share this page:

Suggested articles

0 Comment(s)

Add comment