-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun-sam-local.py
50 lines (43 loc) · 1.82 KB
/
run-sam-local.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
import os, yaml, subprocess, sys, time, socket
from collections import namedtuple
Lambda = namedtuple("Lambda", "path lambdaname")
lambdas = []
def read_templates(paths):
for path in paths:
for f in os.listdir(path):
joined_path = os.path.join(path, f)
if os.path.isdir(joined_path) and os.path.isfile(os.path.join(joined_path, 'template.yaml')):
lambdas.extend(read_lambdas(joined_path))
def read_lambdas(dir):
dir_lambdas = []
with open('{}/template.yaml'.format(dir)) as f:
cf = yaml.safe_load(f)
for key, value in cf['Resources'].items():
if value['Type'] == 'AWS::Lambda::Function' and 'LogRetention' not in key :
dir_lambdas.append(Lambda(path=dir, lambdaname=key))
return dir_lambdas
read_templates(['marine', 'road'])
i = 0
for l in lambdas:
i = i+1
print('{}. {} {}'.format(i, l.path, l.lambdaname))
lambda_run_idx = int(raw_input())
lambda_run_actual_idx = lambda_run_idx - 1
if len(lambdas) - 1 < lambda_run_actual_idx:
print('Lambda with number {} not found'.format(lambda_run_idx))
else:
devnull = open(os.devnull, 'w')
lambda_to_run = lambdas[lambda_run_actual_idx]
p = subprocess.Popen('cd {}; sam local invoke {} -d 9999;'.format(lambda_to_run.path, lambda_to_run.lambdaname),
shell=True,stdin=None,stdout=devnull,stderr=devnull,close_fds=True)
start_time = time.time()
print('Waiting for container to start..')
while True:
debug_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_connect_value = debug_socket.connect_ex(('127.0.0.1',9999))
if socket_connect_value == 0:
break
elif time.time() - start_time > 30:
print('..more than 30 seconds passed, aborting')
sys.exit(1)
print('..done')