-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_dependencies.py
More file actions
34 lines (25 loc) · 1.12 KB
/
lambda_dependencies.py
File metadata and controls
34 lines (25 loc) · 1.12 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
# Lambda that builds dependencies out of the requirements.txt using it's native OS (AL2) and uploads to S3
# Useful for building binary dependencies for Lambdas
import subprocess, shutil, os, zipfile, boto3
BUCKET_NAME = 'BUCKET_NAME'
S3_KEY = 'lambda/build/function.zip'
PYTHON_VERSION = 'python'
build_dir = '/tmp/deps'
zip_path = 'function.zip'
requirements_file = 'requirements.txt'
source_file = 'index.py'
def lambda_handler(event, context):
if os.path.exists(build_dir):
shutil.rmtree(build_dir)
os.makedirs(build_dir)
subprocess.check_call([PYTHON_VERSION, '-m', 'pip', 'install', '-r', requirements_file, '-t', build_dir])
shutil.copy(source_file, build_dir)
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(build_dir):
for file in files:
abs_path = os.path.join(root, file)
rel_path = os.path.relpath(abs_path, build_dir)
zipf.write(abs_path, rel_path)
s3 = boto3.client('s3')
s3.upload_file(zip_path, BUCKET_NAME, S3_KEY)
print(f"Uploaded to s3://{BUCKET_NAME}/{S3_KEY}")