Skip to content

Instantly share code, notes, and snippets.

@richardnbanks
Created October 15, 2017 22:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save richardnbanks/acdd734211912d84485c93311ff6e181 to your computer and use it in GitHub Desktop.
Save richardnbanks/acdd734211912d84485c93311ff6e181 to your computer and use it in GitHub Desktop.
AWS Lambda function to send notifications from CodeDeploy to Slack
from __future__ import print_function
import json, urllib, urllib2
def send_slack(message):
"""
Send Slack Message to Deployments Channel
"""
slack_token = ''
slack_channel = ''
slack_user = 'Deployment Bot'
slack_url = 'https://slack.com/api/chat.postMessage'
message_data = json.loads(message)
fields = []
fields.append({
'title': 'Application',
'value': message_data['applicationName'],
'short': 'true',
})
fields.append({
'title': 'Group',
'value': message_data['deploymentGroupName'],
'short': 'true'
})
fields.append({
'title': 'Deployment ID',
'value': message_data['deploymentId'],
'short': 'true'
})
new_message = {
'text': '',
'color': 'good',
'fields': fields
}
if message_data['status'] == "CREATED":
new_message['color'] = 'warning'
new_message['text'] = 'Started deployment'
elif message_data['status'] == "SUCCEEDED":
new_message['text'] = 'Deployment successful'
elif message_data['status'] == "FAILED":
new_message['color'] = 'danger'
new_message['text'] = 'Deployment failed'
slack_message = [new_message]
payload = {
"token": slack_token,
"channel": slack_channel,
"username": slack_user,
"attachments": json.dumps(slack_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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment