AWS Elastic Beanstalk Slack Notifications

If you’re like me, you use Elastic Beanstalk because it provides a nice Heroku like experience for deploying apps while still having all the flexibility of being on AWS. If you’re even more like me, you like using Slack as a hub for software engineering operations. This is a quick guide to integrate the two.

The Journey

I google’d “elastic beanstalk slack notifications” and didn’t find anything super simple to just get these darn notifications into Slack. I found a paid app that’s $5/mo per user and said “Pshhh nah, that’s almost how much I pay for my actual Slack licenses”. I found some AWS articles that talk about the built in notification SNS topic Beanstalk comes with (which is awesome), but not how to pipe it into Slack. I setup AWS Chat Bot, only to figure out it doesn’t support Beanstalk. Eventually, I came across a Lambda for sending Slack messages via Webhook. That’ll do bot, that’ll do.

The Guide

This is what I ended up doing. It’s probably not the best approach, but it gets the job done.

Slack Webhook

  1. Open up the Slack workspace you want to integrate with and go to Tools > Workflow Builder.

  2. This’ll open up the Workflow Builder window. From there click the green “Create” button. Come up with a name and click “Next”.

  3. Now you’ll be prompted how the workflow gets started, select “Webhook”.

  4. On the next screen, click “Add variable”. Set the key to “message” and the data type should be text. Click “Done” to save the variable and then click the green “Next” button.

  5. Now you’ll be presented with a visualization of the workflow. Click “Add step” and add the “Send a message” step. Select the channel you want to send to. Click “Insert a variable”, select “message”, and click the green “Save” button.

  6. Next click the green “Publish” button in the top right. You’ll get some lovely confetti and your webhook URL. Copy that URL.

AWS Lambda

  1. Open the AWS console, make sure you’re in the right region for your Beanstalk, and go to AWS Lambda.

  2. Click the orange “Create function” button. Leave it on the “Author from scratch” option, give the function a name, and select the Python runtime. You can leave everything else alone and click “Create function”.

  3. Once the function is created, click “Add trigger”. Then pick “SNS” as your source. Next pick the Beanstalk topic you want, they’ll all start with “ElasticBeanstalkNotifications”. Now click “Add”.

  4. Click the “Code” tab and paste in the code below.

  5. Finally, click “Deploy” and enjoy those sweet, sweet Slack notifications.

import urllib3
import json

http = urllib3.PoolManager()

def lambda_handler(event, context):

    msg = {
        "message": event['Records'][0]['Sns']['Message'].split('\n')[1]
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)

NOTE: I split the string because there’s a lot of boilerplate in the messages. If you want everything, you can remove that and pass in the whole message.