forked from aws/aws-health-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Health-Event-Status-LambdaFn.py
47 lines (43 loc) · 1.41 KB
/
Health-Event-Status-LambdaFn.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Health-Event-Status-LambdaFn
# Given an events ARN return if it is open or closed
# Input: Event ARN
# Output: Event Status (open, closed, upcoming)
import json # essential to read json
import os # required to read in the os variables
import boto3 # AWS CLI, required to poll AWS Health
# Static vars
maxEvents=10 # Max is 100, but we expect just 1
# Main lambda function
def lambda_handler(event, context):
# read in the eventArn input
eventArn= event['eventArn']
# Load the AWS Health API
health= boto3.client('health', region_name='us-east-1')
# Pull the event matching the Arn passed in, catch any errors
try:
events_dict= health.describe_events(
filter={'eventArns': [eventArn]},
maxResults=maxEvents
)
except Exception as e:
print(e)
message= 'ERROR: getting events status'
print(message)
raise Exception(message)
# pull out just the events
our_events=events_dict['events']
# now lets validate we received what we expected, 1 result
if (len(our_events)==0):
# Error state, no match
message= 'ERROR: ARN not detected'
print(message)
raise Exception(message)
elif (len(our_events)>1):
# Error state, too many matches
message="ERROR: Multiple ARNs detected"
print(message)
raise Exception(message)
else:
# return the status code for our one event
statusCode= our_events[0]['statusCode']
return statusCode