Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mskutin/91ffc9a50b386b69e557ae18867395cd to your computer and use it in GitHub Desktop.
Save mskutin/91ffc9a50b386b69e557ae18867395cd to your computer and use it in GitHub Desktop.
Lambda function that takes ECS Cloudwatch events and sends a notification to Slack
# Written by Dane Fetterman on 12/1/2016
# https://aws.amazon.com/blogs/compute/monitor-cluster-state-with-amazon-ecs-event-stream/
# Where they use a cloud watch event from ECS to notify a SNS topic. We're
# sending a notification directly to slack instead
import requests
import json
from boto3 import session, client
webhookurl = "https://hooks.slack.com/services/WHATEVERendpointYouGotFromSlack"
def lambda_handler(event, context):
# Establish credentials
session_var = session.Session()
credentials = session_var.get_credentials()
region = session_var.region_name or 'us-east-1'
if "detail-type" not in event:
raise ValueError("ERROR: event object is not a valid CloudWatch Logs event")
else:
if event["detail-type"] == "ECS Task State Change":
detail = event["detail"]
if detail["lastStatus"] == "STOPPED":
if detail["stoppedReason"] == "Essential container in task exited":
# Send an error status message.
url = webhookurl
Subject="ECS task failure detected for container",
Message=json.dumps(detail)
messagedata = json.loads(Message)
# Break out Cloudwatch payload into variables that we use
taskArn = messagedata['taskArn']
desiredStatus = messagedata['desiredStatus']
lastStatus = messagedata['lastStatus']
stoppedReason = messagedata['stoppedReason']
clusterArn = messagedata['clusterArn']
taskDefinitionArn = messagedata['taskDefinitionArn']
# Get all content from nested containers param:
#containers = messagedata['containers']
# Get specific nested content from containers param:
containersreason = messagedata['containers'][0]['reason']
#ALL DATA that was posted from cloudwatch##
#payload = {'channel': '#builds', 'username': 'webhookbot', 'text': '%s ' % (messagedata), 'icon_emoji': 'ghostwn:', 'username': 'ECSLambda-Post-Bot', 'channel': '#builds' }
#Only post to slack with specific data from cloudwatch ##
payload = {'channel': '#builds', 'username': 'webhookbot', 'text': '%s \n Task ARN: %s \n Desired Status: %s \n Last Status: %s \n Stopped Reason: %s \n Reason: %s \n Cluster ARN: %s ' % (Subject, taskArn, desiredStatus, lastStatus, stoppedReason, containersreason, clusterArn, ), 'icon_emoji': 'ghostwn:', 'username': 'ECSLambda-Post-Bot', 'channel': '#builds' }
headers = {"content-type": "application/json" }
r = requests.put(url, data=json.dumps(payload), headers=headers)
#print json.dumps(messagedata)
print r.status_code
print r.content
note = "Post of data to slack was attempted"
# some resources:
#https://api.slack.com/incoming-webhooks
#http://requestb.in/pehntype
#http://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment