How To Set Up A Slack Channel To Be An AWS SNS Subscriber

The Slack-SNS-Elastic Beanstalk Hookup

Joseph Terranova
May 13, 2015 · 4 min read

Overview

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:

Image for post
Image for post
Elastic Beanstalk Environment Configuration: Notifications Options

Will manifest itself in SNS as something like this:

Image for post
Image for post
SNS Topic & Subscription created by Elastic Beanstalk

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.

Configure Slack

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:

Image for post
Image for post
Your Slack Integrations

Add a new Incoming WebHooks integration, selecting the appropriate Slack channel:

Image for post
Image for post
New Incoming WebHooks Integration

After adding the integration, you should be presented with Setup Instructions and your Webhook URL:

Image for post
Image for post
WebHook Integration Settings

You’re done configuring Slack!

Configure AWS

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

Huh?

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:

Image for post
Image for post
Define Lambda Function Name

In the Lambda function code section, choose the None option for Code template. In the code textbox, paste this:

Function notes:

  • 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.

Image for post
Image for post
Choosing Lambda Function Role

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:

Image for post
Image for post
Test Lambda 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:

Image for post
Image for post
Slack: Message Received!

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:

Image for post
Image for post
Create SNS Subscription

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:

Image for post
Image for post
Updated SNS Topic Subscriptions

Caveat Emptor

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…

References

Props:

AWS:

Slack:

Cohealo Engineering

Cohealo is a technology company that helps health systems…

Joseph Terranova

Written by

I make things go. 火贼.

Cohealo Engineering

Cohealo is a technology company that helps health systems share medical equipment across facilities, so they can optimize spend, increase the utilization of procured equipment, and improve access to care.

Joseph Terranova

Written by

I make things go. 火贼.

Cohealo Engineering

Cohealo is a technology company that helps health systems share medical equipment across facilities, so they can optimize spend, increase the utilization of procured equipment, and improve access to care.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store