-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambdaUpdate.py
More file actions
63 lines (55 loc) · 2.03 KB
/
lambdaUpdate.py
File metadata and controls
63 lines (55 loc) · 2.03 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
import argparse
from zipfile import ZipFile
import boto3
import io
import json
import os
def main():
parser = argparse.ArgumentParser(description='Update Lambda function')
parser.add_argument('-fn', help='Function name', required=True)
parser.add_argument('-files',help='List of files to combine into a package',nargs='+', required=True)
parser.add_argument('-s3',help='The S3 bucket to be used as staging (for larger lambda functions)')
args = parser.parse_args()
obj = io.BytesIO()
with ZipFile(obj, 'w') as zip_object:
row = 0
for f in args.files:
if os.path.isdir(f):
print(f"{f} is a folder")
for r, d, F in os.walk(f):
for file in F:
x = os.path.join(r, file)[len(f)+1:]
print(f" - Zipping -- {f}/{x}")
zip_object.write(f"{f}/{x}",x)
#print(x)
else:
# Adding files that need to be zipped
print(f" - Zipping -- {f}")
if row == 0:
filename = 'index.py'
else:
filename = f
zip_object.write(f,filename)
row += 1
# -- update the lambda function's code
if not args.s3:
response = boto3.client('lambda').update_function_code(
FunctionName=args.fn,
ZipFile=obj.getvalue(),
Publish=True
)
print(json.dumps(response,indent=4))
else:
print("Uploading to S3...")
S3Key = "f{args.fn}.zip"
obj.seek(0)
s3 = boto3.resource('s3').Bucket(args.s3).upload_fileobj(obj, S3Key)
response = boto3.client('lambda').update_function_code(
FunctionName=args.fn,
S3Bucket=args.s3,
S3Key=S3Key,
Publish=True
)
print(json.dumps(response,indent=4))
if __name__ == '__main__':
main()