#!/usr/bin/env python # coding: utf-8 # ## Make AWS Layer! # This notebook automates the (re)build, install, zip, upload to S3, and aws cli command to publish new version to an AWS layer. # In[1]: from my_utils.os_utils import subprocess_execute import shutil # In[2]: # list of packages to build packages_todo = [ # list of tuples: ('package_name','from_pip') ('pandas',True), ('my_utils',False), ('my_dev',False), ] package_base_dir = '/home/jovyan/packages' work_dir = '/home/jovyan/work' s3_path = 's3://andrewm4894-python-packages/latest' s3_region = 'us-west-2' s3_bucket = s3_path.split('s3://')[1].split('/')[0] s3_url = f"https://s3.console.aws.amazon.com/s3/buckets/{s3_path.replace('s3://','')}?region={s3_region}&tab=overview" aws_layer_name = 'my-aws-python-packages' # ## Build & Install Packages # In[3]: # build and install locally each package for package_name, from_pip in packages_todo: cmd = '' # run pip install within the python dir if from_pip: # if from pip then just use name cmd = cmd + f'pip install --upgrade --force-reinstall "{package_name}" -t {work_dir}/python/python/.' else: # cd into the package dir cmd = f'cd {package_base_dir}/{package_name}' # run the build commands to create local egg file to be zipped up into an AWS layer cmd = cmd + f' && python setup.py bdist_egg' # then do a local install cmd = cmd + f' && pip install --upgrade "{package_base_dir}/{package_name}" -t {work_dir}/python/python/.' # kick off big command to do the stuff result = subprocess_execute(cmd) # look at results print(result) # ## Create python.zip # In[4]: # make zip shutil.make_archive(f'{work_dir}/python', 'zip', f'{work_dir}/python') # ## Upload to S3 # In[5]: # upload zip to s3 using aws cli cmd = f'aws s3 cp --region {s3_region} {work_dir}/python.zip {s3_path}/' subprocess_execute(cmd) print(f'... zip should be updated at: {s3_url} ...') # ## Publish to AWS Layer # In[6]: # publish zip file from s3 location to aws layer cmd = f"aws lambda publish-layer-version" cmd += f" --layer-name {aws_layer_name}" cmd += f" --content S3Bucket={s3_bucket},S3Key={s3_path.split('/')[-1]}/python.zip" cmd += f" --region {s3_region}" subprocess_execute(cmd) # In[ ]: