-
Notifications
You must be signed in to change notification settings - Fork 247
/
LambdaFunction.py
27 lines (26 loc) · 1.13 KB
/
LambdaFunction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Sample Lambda Function to post notifications to a Chime room when an AWS Health event happens
from __future__ import print_function
import json
import logging
import os
from urllib2 import Request, urlopen, URLError, HTTPError
# Setting up logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
# main function
def lambda_handler(event, context):
"""Post a message to the Chime Room when a new AWS Health event is generated"""
message = str(event['detail']['eventDescription'][0]['latestDescription'] + " https://phd.aws.amazon.com/phd/home?region=us-east-1#/event-log?eventID=" + event['detail']['eventArn'])
json.dumps(message)
chime_message = {'Content': message}
logger.info(str(chime_message))
webhookurl = str(os.environ['CHIMEWEBHOOK'])
req = Request(webhookurl, json.dumps(chime_message))
try:
response = urlopen(req)
response.read()
logger.info("Message posted: %s", chime_message['Content'])
except HTTPError as e:
logger.error("Request failed : %d %s", e.code, e.reason)
except URLError as e:
logger.error("Server connection failed: %s", e.reason)