I love Slack. I also love Amazon Elastic Beanstalk (EB). When you create a new EB environment and have supplied an email address for receiving notifications, EB will set up a new Simple Notification Service (SNS) Topic and subscriber.
So something like this:
Will manifest itself in SNS as something like this:
Thus begins the steady flow of “AWS Elastic Beanstalk Notification” emails to you and/or your team. Yup. More emails…
Slack It, SNS
Codeship has a nice model for Slacking build status. If you haven’t seen it, here’s a glimpse. This is what we’ll implement: a Slack channel that displays the contents of “AWS Elastic Beanstalk Notification” emails, along with good, warning, or danger color-coding to denote health/status.
Create a new channel to receive SNS notifications (e.g., aws-sns).
Next, configure Slack Integrations. Typically, this is available at https://your-domin-name-goes-here.slack.com/services/new:
Add a new Incoming WebHooks integration, selecting the appropriate Slack channel:
After adding the integration, you should be presented with Setup Instructions and your Webhook URL:
You’re done configuring Slack!
On the AWS side, create a Lambda function (yup!) and configure it to be an SNS subscriber to an existing Topic. This article assumes you are using an SNS Topic created by EB.
Create Lambda Function
AWS Lambda runs your code on a high-availability compute infrastructure and performs all the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code and security patch deployment, and code monitoring and logging. All you need to do is supply your Node.js code.
In the AWS console, go to Lambda and create a new Lambda function.
In the Lambda function name section, do this:
In the Lambda function code section, choose the None option for Code template. In the code textbox, paste this:
- Be gentle, I know you’re a better Node.js developer than I am.
- The icon_emoji refers to a custom AWS emoji. Use any of the standard Slack emoji, or roll your own (see References below).
- Make sure you supply your Webhook URL for the path value.
For Role, either create a new or choose an existing role. The first time you create a function, choose the option to create the Basic execution role. The next time you do this you will be able to select the existing role, lambda_basic_execution.
After creating the Lambda function, you should be taken back to the Lambda Function List screen.
Test Lambda Function
On the Lambda Function List screen, click on your function and choose Edit or test function:
In the Sample event textbox, paste the following sample SNS message:
Click the Invoke button, and you should be doing the Slack happy dance:
Hook Up SNS To Lambda Function
Go to the SNS console and select the Topic you would like your Lambda function to subscribe to. Choose the Subscribe to topic Action:
In the Create Subscription dialog:
- For the Protocol value, select AWS Lambda.
- For the Endpoint value, select your Lambda function.
- Click the Create subscription button.
Your SNS Topic details should look a little different now:
Because EB is a set of managed services, when you terminate an EB environment said services are also terminated/deleted (e.g., EC2 instances, Elastic Load Balancers, SNS Topics, etc…). You’ll need to implement your own process for maintaining the Lambda function’s SNS Subscription in the event the SNS Topic it subscribes to is deleted.
Make It So
Deploy a new application version to EB, or do whatever it is you need to do to trigger the SNS Topic your Lambda function subscribes to. Watch the messages flow into Slack…
- esanchezm/amazon-cloudwatch-to-slack: Amazon CloudWatch to Slack for suggesting AWS Lambda.
- Coffee’s for Closures for noticing esanchezm’s Lambda comment in the Issues List, and for helping get the Lambda function’s Node.js code just right.