h-imaoka
4/17/2018 - 5:28 AM

ecs-event to slack

ecs-event to slack

import os
import logging
import requests
import json

logger = logging.getLogger()
logger.setLevel(logging.INFO)
env_webhook = os.environ['SLACK_WEBHOOK']
env_channel = os.environ['SLACK_CHANNEL']


def notify_slack(body):
    requests.post(
        env_webhook,
        data=json.dumps({
            'text': body,
            'username': 'aws-ecs-notify',
            'icon_emoji': ':docker:',
            'link_names': 1,
            'channel': env_channel,
            })
        )


def handle(event, context):
    try:
        sns_body = '```' + json.dumps(event, indent=2) + '```'
        notify_slack(sns_body)

        return 0
    except Exception as e:
        logger.error(e)
        raise e