Skip to content

Instantly share code, notes, and snippets.

@mattmcd
Created March 20, 2018 17:27
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mattmcd/e27b143861a8fa825db550304e95d48a to your computer and use it in GitHub Desktop.
Hook for AWS Lambda function to retrieve library files from S3 and add to local path in tmp directory
import boto3
import os
import sys
import zipfile
HERE = os.path.dirname(os.path.realpath(__file__))
def library_install():
"""Dynamically add libraries to path, retrieving from S3 if necessary
"""
vendored_file = 'tf_env.zip'
DATA_DIR = '/tmp'
DATA_BUCKET = 'build_lambda'
REGION_NAME = 'eu-west-1'
local_vendored = os.path.join(DATA_DIR, vendored_file)
remote_key = 'tensorflow_0p12/' + vendored_file
LIB_DIR = os.path.join(DATA_DIR, 'vendored')
if not os.path.isfile(local_vendored):
print('Connecting to S3')
s3 = boto3.resource('s3', region_name=REGION_NAME)
bucket = s3.Bucket(DATA_BUCKET)
print('Copying vendored libraries to local /tmp')
bucket.download_file(remote_key, local_vendored)
print('Libraries copied to local /tmp')
zipref = zipfile.ZipFile(local_vendored)
zipref.extractall(DATA_DIR)
zipref.close()
print('Appending libraries to path')
sys.path.append(DATA_DIR)
print(sys.path)
print('Appended libraries to path')
else:
print('Libaries already copied locally')
if LIB_DIR not in sys.path:
sys.path.append(LIB_DIR)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment