-
-
Save vvalorous/cfdf0b7127e3c5f714c403012965d0fc to your computer and use it in GitHub Desktop.
AWS Lambda function to send notifications from CodeDeploy to Slack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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