-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploader.py
More file actions
51 lines (39 loc) · 1.81 KB
/
uploader.py
File metadata and controls
51 lines (39 loc) · 1.81 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
import boto3
import ConfigParser
import argparse
import os
import zipfile
import glob
#Setting up arguments for script
parser = argparse.ArgumentParser(description='Easy to use function upload tool for Amazon Lambda')
parser.add_argument('--coffee', help='argument to change coffescript files to js files before upload', action='store_true')
parser.add_argument('--config', help='specify path to configuration file. Example: /root/functions.ini', required=True)
parser.add_argument('--function', help="function from config file to upload to lambda")
args = parser.parse_args()
#Read in Configuration File
Config = ConfigParser.ConfigParser()
Config.read(args.config)
#If Files are Coffee
if args.coffee == True:
os.system("coffee --compile " + Config.get(args.function, "dirPath"))
#Change to directory of function and build Zip File
os.chdir(Config.get(args.function, "dirPath"))
zipFile = args.function+".zip"
zf = zipfile.ZipFile(zipFile, mode='w')
files = glob.glob("*.js")
for file in files:
zf.write(file)
for dirname, subdirs, files in os.walk("node_modules"):
zf.write(dirname)
for filename in files:
zf.write(os.path.join(dirname, filename))
zf.close()
#Setup AWS Session to Lambda
session = boto3.Session(aws_access_key_id=Config.get("aws", "aws_access_key_id"),
aws_secret_access_key=Config.get("aws", "aws_secret_access_key"),
region_name=Config.get("aws", "region_name"))
client = session.client('lambda')
#Zipfile has to be sent as binary
fileUpload = open(zipFile)
#Upload Zip to Lambda
client.upload_function(FunctionName=Config.get(args.function, "Name"), FunctionZip=fileUpload, Runtime=Config.get(args.function, "Runtime"), Role=Config.get(args.function, "Role"), Handler=Config.get(args.function, "Handler"), Mode=Config.get(args.function, "Mode"))