AWS Lambda function to monitor SQS queue to a Slack channel
There are some SQS queues on amazon (+/- 10 queues, but the number of queues is growing every month). Queues are for different applications, different teams, and have different characteristics (error queues, different purposes, different thresholds etc.). The teams are responsible for adding new queues and specifying which queues are applicable to be monitored.
As a DevOps team we would like to monitor these queues by providing an automation solution which creates a monitor per queue according to a definition file where the queues are specified. So that we can be alerted whenever something goes wrong with the queues.
Most IT people prefer Slack as a communication platform between teams, sending a notification to a team Slack channel is a good way to integrate between the alarting system and team comunication.
For definition file, I prefered to use
YAML is a human-readable structured data format. It is less complex and ungainly than XML or JSON, but provides similar capabilities. It essentially allows you to provide powerful configuration settings.
A team can define thier queues in this simple YAML format:
- team2:
- test_devops_new_houses: 10
- test_devops_edited_houses: x
- test_devops_removed_houses: x
- test_devops_new_houses_errors: 0
- test_devops_edited_houses_errors: 0
- test_devops_removed_houses_errors: 0
The ERROR queue will have limit value 0
, where you can use x
for the queue you want to skip.
AWS Lambda function will run every minute to check the defined queue in the YAML file stored in GITHUB, where stakeholder can edit it easily.
Setup steps:
- Create Slack App and allow it to post to a team channel.
- Create AWS Queues then IAM user and get the secret key.
- Edit the keys in Lambda source code.
- Create AWS Lambda function and delopy the code.
- Test it.
- let's say you have the following Slack channels:
team1
,team2
,team3
; where the channel name is the same as your team name defined in the YAML file. if not, let's create one:
- Then click on
+ Add an app
, or go to channel setting if you didn't find the link:
- This will open Slack Admin panel on the browser; you have to have admin permission on your Slack team to access it, click on
Build
:
- Click
Start Building
thenCreate App
, chose your team and name the appAWS-Watcher
; if you wish to have a diffrent name you should change the name in the source code as well:
- On
Basic Information
clickIncoming Webhooks
underAdd features and functionality
:
- Activate it by swiping to
On
, then click the bottom buttonAdd New Webhook to Workspace
- Select your team channel, to allow the app to post on it:
- Repeat the steps for all your teams defined in the YAML file, then copy all the Webhook URLs, we gonna use it in Lambda source code:
- Go Back to
Basic Information
:
- Under
Display Information
add description, icon and backgorund color to your Slack App, then hitSave Changes
:
- Finally, go back to your team Slack channel, you should see the integration message:
- On AWS Console, select a region, then
SQS
and create your queues:
- Again on AWS Console, go to
IAM
, and create a new user withProgrammatic access
:
- Click Next, select
Attach exisiting policies directly
, search forsqs
, then selectAmazonSQSFullAccess
, this will allow this user to access the queues:
- Click Next, copy the Access Key ID and Secret access key, we gonna use them in AWS Lambda function:
- Clone or Download this repo, open
index.js
, on top of the file you need to edit the variables of Slack Webhook URL and AWS keys:
- On AWS Console, select a region, go to Lambda, click on
Author from scratch
:
- Name the function
monitor-sqs-slack
, selectChoose an existing role
on Role, andservice-role/sqspoller
for Existing role, then hitCreate function
:
- Compress the file
index.js
and the directorynode_modules
to a zip file:
- Back to AWS console, on
Code entry type
, selectUpload a .ZIP file
, select the ZIP file from your machine:
- On
Triggers
, click,Add trigger
:
- On
Rule
, selectCreate a new rule
, forRule name
typeeverymin
, forRule type
chooseSchedule expression
, typerate(1 minute)
, then clickSubmit
:
- Click
Save
:
Congrats, you are ready to test now!!
You can test using AWS Console or AWS CLI, I will show you how to test using the CLI:
- Download
aws-cli
, on Mac OS X, you can type this command if you have Brew
brew install aws-cli
- Add AWS secret keys to aws cli:
aws configure
- Check if you can list SQS queues:
aws sqs list-queues
- Try to send message to one of the ERROR queues:
aws sqs send-message --queue-url https://eu-west-1.queue.amazonaws.com/223381404055/test_devops_edited_houses_errors --message-body "this is an error message" --message-attributes file://msg.json
- In a minute, your should see the alert on your team Slack channel: