-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda.py
More file actions
90 lines (66 loc) · 3.21 KB
/
lambda.py
File metadata and controls
90 lines (66 loc) · 3.21 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import boto3
import json
import requests
import argparse
from argparse import RawTextHelpFormatter
from botocore.exceptions import ClientError
from common import load_config_json
from common import print_table
parser = argparse.ArgumentParser(description='[*] Lambda function uploader.\n'
'[*] Specify the zip file to upload with the function name and runtime environment.'
' \n\nusage: \n python lambda.py -f <FileName> -func <FunctionName> -r <RunTimeEnv>', formatter_class=RawTextHelpFormatter)
required = parser.add_argument_group('required arguments')
required.add_argument('-f', '--fileName', help='The name of the zip file containing your deployment package.', required=True)
required.add_argument('-func', '--functionName', help='The name you want to assign to the function you are uploading.', required=True)
required.add_argument('-r', '--runTime', help='The runtime environment for the Lambda function you are uploading. E.g.: python2.7', required=True)
args = vars(parser.parse_args())
def init():
# If the config file cannot be loaded then boto3 will use its cached data because the global variables contain nonesens ("N/A")
config_parsing_was_successful, region_name_for_logs = load_config_json("conf.json")
if not config_parsing_was_successful:
region_name_for_logs = "N/A"
lambda_client = boto3.client('lambda', region_name=region_name_for_logs)
r = requests.get('http://169.254.169.254/latest/meta-data/iam/info')
role_arn = json.loads(r.text)['InstanceProfileArn']
return lambda_client, role_arn
def list_functions(lambda_client):
try:
functions = lambda_client.list_functions()
values = []
for func in functions['Functions']:
values.append([func['FunctionName'], func['Runtime'], func['Description']])
except Exception as e:
print('Error: {}'.format(e))
return values
def create_run_function(lambda_client, role_arn):
role_arn_mod = ':'.join(role_arn.split(':')[:5]) + ':role/' + role_arn.split('/')[1]
try:
with open(args['fileName'], 'rb') as f:
zipped_code = f.read()
except Exception as e:
print('Commandline specified file could not be loaded: {}'.format(e))
try:
lambda_client.create_function(
FunctionName=args['functionName'],
Runtime=args['runTime'],
Role=role_arn_mod,
Handler='main.handler',
Code={'ZipFile': zipped_code}
)
print('\nThe new Lambda function is uploaded.')
except ClientError as ce:
if ce.response['Error']['Code'] == 'InvalidParameterValueException':
# print('Error: Could not unzip uploaded file. Please check your file, then try to upload again.')
print(ce)
try:
lambda_client.invoke(FunctionName=args['functionName'])
except Exception as e:
print(e)
def main():
lambda_client, role_arn = init()
values = list_functions(lambda_client)
print('\nThe existing functions in Lambda:')
print_table(values, ['FunctionName', 'Runtime', 'Description'])
create_run_function(lambda_client, role_arn)
if __name__ == '__main__':
main()