-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.py
41 lines (31 loc) · 1.12 KB
/
schedule.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
#!/usr/bin/env python27
import os
import sys
import boto3
import json
import shortuuid
schedule_mins = 15
function_name = "RedshiftUtilsSnapshotManager"
def schedule(args):
config = args[0]
function_arn = args[1].replace('"', '')
cw = boto3.client('events', region_name=os.environ['AWS_REGION'])
# read the supplied configuration file
config_file = open(config, 'r')
json_config = json.load(config_file)
namespace = json_config['namespace']
rulename = "%s.%s-%s-mins" % (function_name, namespace, schedule_mins)
# create the cloudwatch event rule
rule = cw.put_rule(Name=rulename,
ScheduleExpression='rate(%s minutes)' % (schedule_mins),
State='ENABLED')
print "Created CloudWatch Rule Definition %s\n" % (rule["RuleArn"])
# add the CW target
target = {}
target["Id"] = "%s" % (namespace)
target["Arn"] = function_arn
target["Input"] = json.dumps(json_config)
target = cw.put_targets(Rule=rulename, Targets=[target])
print "Linked Lambda Function %s to Rule\n" % (function_arn)
if __name__ == "__main__":
schedule(sys.argv[1:])