Setup Slack Notifications for AWS CodeDeploy

CodeDeploy doesn’t come with a native way to integrate with third party services; so in order to integrate with a service you need to do a small workaround involving SNS and Lambda.

Setup SNS topic and Lambda

First you need to setup a SNS Topic “deployments” (Feel free to use your own topic name). Once that is done you need to hop on over to Lambda and find a SNS blueprint, I used sns-message-python with the event SNS and then set to the topic just created.

Next setup the basic execution role recommended by AWS (if you don’t have a role setup already).

Create your Lambda Function

from __future__ import print_function
import json, urllib, urllib2

def send_slack(message):
    """
    Send Slack Message to Deployments Channel
    """
    slack_token = '<api_token>'
    slack_channel = '<channel_short_name>'
    slack_user = 'Deployment Bot'
    slack_url = 'https://slack.com/api/chat.postMessage'
    payload = {
        "token": slack_token,
        "channel": slack_channel,
        "username": slack_user,
        "text": message
    }
    query_string = urllib.urlencode(payload)
    url = slack_url + '?' + query_string
    response = urllib2.urlopen(url)

def lambda_handler(event, context):
    message = event['Records'][0]['Sns']['Message']
    send_slack(message)
    return message

Create a Trigger on your CodeDeploy

I used the following triggers

  1. Deployment Starts
  2. Deployment Succeeds
  3. Deployment Fails

That’s it now everytime you deploy using CodeDeploy you can have a tracking log via Slack.

I am a huge fan of integrating slack with everything from saltstack to monitoring alerts. That way everything is in one place!

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.