Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save threadstonesecure/7fe4082ab7b21f29a347d7f1ca40bb24 to your computer and use it in GitHub Desktop.
Save threadstonesecure/7fe4082ab7b21f29a347d7f1ca40bb24 to your computer and use it in GitHub Desktop.
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