Created
March 20, 2018 17:27
Star
You must be signed in to star a gist
Hook for AWS Lambda function to retrieve library files from S3 and add to local path in tmp directory
This file contains 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 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