The Lambda deployment package size limit cannot exceed 250 MB.
Create Lambda Layer Install the library using Lambda's [/ tmp] folder (512M) This time, I will explain method 2.
requirements-lambda.txt
numpy==1.16.3
create.sh
#!/bin/bash
if [ -d "deploy" ]; then rm -Rf deploy; fi
mkdir deploy
pip install -r requirements-lambda.txt -t deploy/requirements-lambda/
cd deploy/requirements-lambda
rm -r PIL
rm -r Pillow*
zip -9 -r ../lambda-requirements.zip .
cd ..
rm -r requirements-lambda
handle.py
import os
import sys
import zipfile
pkgdir = '/tmp/requirements'
zip_requirements = 'lambda-requirements.zip'
if os.environ.get("AWS_EXECUTION_ENV") is not None:
if not os.path.exists(pkgdir):
root = os.environ.get('LAMBDA_TASK_ROOT', os.getcwd())
zip_requirements = os.path.join(root, zip_requirements)
zipfile.ZipFile(zip_requirements, 'r').extractall(pkgdir)
sys.path.append(pkgdir)
handle.py
import boto3
import os
import sys
import zipfile
REQUIREMENTS_BUCKET_NAME = ''
REQUIREMENTS_KEY = ''
pkgdir = '/tmp/requirements'
zip_requirements = '/tmp/lambda-requirements.zip'
sys.path.append(pkgdir)
if os.environ.get("AWS_EXECUTION_ENV") is not None:
if not os.path.exists(pkgdir):
s3 = boto3.resource('s3')
bucket = s3.Bucket(REQUIREMENTS_BUCKET_NAME)
bucket.download_file(REQUIREMENTS_KEY, zip_requirements)
zipfile.ZipFile(zip_requirements, 'r').extractall(pkgdir)
os.remove('zip_requirements')
sys.path.append(pkgdir)
Recommended Posts