generated from ik-serverless/serverless-blueprint
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
53 lines (44 loc) · 1.61 KB
/
function.py
File metadata and controls
53 lines (44 loc) · 1.61 KB
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
48
49
50
51
52
53
import boto3
import json
#set region
REGION = 'us-west-2'
#set the SNS topic ARN you want to alert on
SNS_TOPIC_ARN = 'arn:aws:sns:REGION:ACCOUNT_ID:TOPIC_NAME'
step = boto3.client('stepfunctions')
def get_state_machine(name=None):
"""
Get a state machine given its name
"""
response = step.list_state_machines()
if not response.get('stateMachines'):
return None
for sm in response.get('stateMachines'):
if sm['name'] == name:
return sm['stateMachineArn']
return response.get('stateMachines')
def get_executions(arn):
"""
Get a state machine executions
"""
response = step.list_executions(
stateMachineArn=arn,
statusFilter='RUNNING',
maxResults=400
)['executions']
print(json.dumps(response, indent=2, sort_keys=True, default=j_serial))
print(len(response))
def j_serial(o): # self contained
from datetime import datetime, date
return str(o).split('.')[0] if isinstance(o, (datetime, date)) else None
def push_message(topic, message):
sns_body = 'step functions running "{}", when suppose to be "{}", at "{}" UTC'.format(event['detail']['instance-id'], event['detail']['state'], event['time'])
client = boto3.client('sns', region_name=REGION)
response = client.publish(
TopicArn=topic,
Subject='Step Function State Change Notification',
Message=message
)
print(json.dumps(get_state_machine(), indent=2, sort_keys=True, default=j_serial))
for e in get_state_machine():
get_executions(e['stateMachineArn'])
# write to logs as well how many step functions currently running