Forked from exy02/awsLambda-DownloadAnyPythonPackage.py
Created
February 28, 2022 09:13
-
-
Save threadstonesecure/7fe4082ab7b21f29a347d7f1ca40bb24 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import shutil | |
import boto3 | |
import json | |
import os | |
def lambda_handler(event, context): | |
#Parameters ============================= | |
pipPackage = "requests" | |
packageVersion = "2.26.0" | |
s3Bucket = "the-pip-eagle-has-landed" | |
#======================================== | |
#Create standard layer directory structure (per AWS guidelines) | |
os.makedirs("/tmp/python/python/lib/python3.9/site-packages/") | |
#Pip install the package, then zip it | |
subprocess.call(f'pip3 install {pipPackage}=={packageVersion} -t /tmp/python/python/lib/python3.9/site-packages/ --no-cache-dir'.split(), stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) | |
shutil.make_archive("/tmp/python", 'zip', "/tmp/python") | |
#Put the zip in your S3 Bucket | |
S3 = boto3.resource('s3') | |
try: | |
S3.meta.client.upload_file('/tmp/python.zip', s3Bucket, f'{pipPackage}/{packageVersion}/python.zip') | |
except Exception as exception: | |
print('Oops, Exception: ', exception) | |
return {'statusCode': 200,'body': json.dumps('Success!')} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment