+86 13541016684Mon. - Fri. 10:00-22:00

Setup Slack Notifications for AWS CodeDeploy

Setup Slack Notifications for AWS CodeDeploy

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

Below is the code I used to send a message to slack using Lambda:

  1. from __future__ import print_function
  2. import json, urllib, urllib2
  3.  
  4. def send_slack(message):
  5. “””
  6. Send Slack Message to Deployments Channel
  7. “””
  8. slack_token = ‘<api_token>’
  9. slack_channel = ‘<channel_short_name>’
  10. slack_user = ‘Deployment Bot’
  11. slack_url = ‘https://slack.com/api/chat.postMessage’
  12. payload = {
  13. “token”: slack_token,
  14. “channel”: slack_channel,
  15. “username”: slack_user,
  16. “text”: message
  17. }
  18. query_string = urllib.urlencode(payload)
  19. url = slack_url + ‘?’ + query_string
  20. response = urllib2.urlopen(url)
  21.  
  22. def lambda_handler(event, context):
  23. message = event[‘Records’][0][‘Sns’][‘Message’]
  24. send_slack(message)
  25. 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!