-
Notifications
You must be signed in to change notification settings - Fork 6
/
runner.py
53 lines (48 loc) · 1.49 KB
/
runner.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
48
49
50
51
52
53
import boto3
import json
import logging
import os
import sys
import time
sys.path.insert(0, './vendor')
from sts import establish_role
from boto3.dynamodb.conditions import Key, Attr
environment = os.environ['ENV']
dynamodb = boto3.resource('dynamodb')
client = boto3.client('ec2')
lambda_client = boto3.client('lambda')
function_name = os.environ['run']
logger = logging.getLogger()
logger.setLevel(logging.INFO)
accounts_table = dynamodb.Table(os.environ['DYNAMODB_TABLE_ACCOUNTS'])
accounts = accounts_table.scan()['Items']
regions = ([region['RegionName']
for region in client.describe_regions()['Regions']])
def invoke_process(fuction_name, account_id, region):
"""Launch target fuction_name on account_id and region
"""
invoke_payload = (
json.JSONEncoder().encode(
{
"account": account_id,
"region": region
}
)
)
lambda_client.invoke(
FunctionName=fuction_name,
InvocationType='Event',
Payload=invoke_payload,
)
def runner(event, context):
"""Launch child import processes on a per account, per region basis
"""
logger.info('Running available_ips...')
for region in regions:
for acct in accounts:
logger.info(
"""
cidr-house-rules-{0}-available_ips on account {1} in region {2}
""".format(environment, acct['id'], region)
)
invoke_process(function_name, acct['id'], region)