Introduction

This tutorial will help you set up a monitor using AWS Lambda that will check whether your RabbitMQ queue has at least one consumer, at a frequency of your choosing, and will send an email if your queue is no longer being consumed.

There are 3 steps to creating queue consumer alerts:

  1. Create the Lambda Function - This is the most complex step. Lambda is an AWS system where you can set up a recurring script. We've provided the code you'll need below, but there are a lot of fields to fill out. The code will make sure your queue is being consumed.

  2. Create the CloudWatch Alarm - In this step, you list the emails that will receive notification when the Lambda Function detects that data in your queue isn't being consumed.

  3. Test and Enable the Setup - Make sure everything is working, and then start the monitor.

Create the Lambda Function

Sign in to the AWS Management Console and open the AWS Lambda console.

In the upper right, change your region to US East (N. Virginia).

Choose Create function and select Blueprints.

Use the search bar under Blueprints to find the lambda-canary blueprint and select it.

Under the Basic Information heading, fill out the following fields:

  • Name - A name for your function (e.g. queueNameMonitor).

  • Role - Choose Create new role from template(s).

  • Role name - A name for your role (e.g. queueMonitorRole).

  • Policy templates - Leave this blank.

Under the CloudWatch Events heading, fill out the following fields:

  • Rule - Choose Create a new rule.

  • Rule name - Type a name for your rule (e.g. queueNameMonitorRule).

  • Rule description - Optional description to remind yourself what this rule is.

  • Rule type - Choose Schedule expression.

  • Schedule expression - How often you want to check on the queue. To check once every half hour, you could type rate(30 minutes). To check once every two hours, you could type rate(2 hours).

  • Enable trigger - Leave this unchecked for now.

Find the Lambda function code heading, and then the Environment variables subheading. Remove the existing variables, and add the following key, value pairs:

Then, click Create function.

On the next screen, ensure the Configuration tab is selected.

Find the Function code heading. Set the following fields:

In the top right corner, hit the Save button.

Once it's done saving, in the top right corner, hit the Test button.

In the modal that pops up, choose Create new test event. Event template should be set to Amazon CloudWatch. Under Event name, type a name for your test event (e.g. queueNameTestEvent). 

Hit the Create button in the bottom right of the modal.

In the top right corner of the screen, hit the Test button again.

One of two things will happen, depending on whether your consumer is set up on the queue or not. 

  • If your consumer is running, you should see a green box appear under the Test button that says Execution result: succeeded.

  • If your consumer is not running, you should see a red box appear under the Test button that says Execution result: failed.

This is a good opportunity to test that everything is set up properly if you’re able to toggle your consumer on and off.

Create the CloudWatch Alarm

Open the CloudWatch console.

In the navigation pane on the left, click on Alarms, then click the Create Alarm button. 

On the modal, under Lambda Metrics, click on By function name.

Choose the row that has the name of the Lambda function you created with the word "Errors" in the Metric Name column. Then, click Next.

Under the Alarm Threshold heading, type a unique name and description for the alarm, and set Whenever to be Errors is >= 1 for 1 consecutive period(s).

Under the Actions heading, look for Send notification to field, and click on the New list link next to it. Enter a topic name, and a list of comma separated emails to be notified of problems with your queue consumer.

Click Create alarm.

Each email address entered will receive a confirmation email. Click the link in the email to confirm your subscription.

Test and Enable the Setup

Return to the AWS Lambda console and click on Functions in the left menu.

Click on the name of your Lambda Function.

If you’re able to turn your consumer on and off, you can test your function again.

  1. With the consumer on, pressing the Test button will give you a green box that says Execution result: succeeded. No email will be sent.

  2. With the consumer off, pressing the Test button will give you a red box that says Execution result: failed. An email will be sent.

Once you're finished testing your setup, it's time to enable the scheduled trigger. Within the Designer section, click on the CloudWatch Events button. This will open a new section below, called CloudWatch Events. Find the trigger rule you created above, and toggle it from Disabled to Enabled by clicking on the switch to the right. Then, hit Save.

Conclusion

That’s it! Your lambda function will now check on your queue at whatever rate you specified, and will email the list provided whenever the queue does not have a consumer.

Did this answer your question?