Unfortunately, you can't upload a deployment to Lambda that has an uncompressed size of 250MB even when you use Layers (Read Note). There's a workaround to this, but it will impact your Lambda performance a lot.
- Write a driver function in Lambda for your code.
- This driver function will download your code from S3 into ephemeral Lambda Storage (/tmp/) and then run it.
Eventual solution is to use ECS Fargate (Serverless) or AWS Batch.
Adding a code sample for Lambda based solution:
import boto3
import os
import subprocess
import sys
sys.path.insert(1, '/tmp')
def download_code_from_s3(bucketName, file_path):
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket(bucketName)
bucket.download_file(file_path, os.path.join('/tmp/', file_path))
def lambda_handler(event, context):
#Download zip file from S3 to /tmp/
download_code_from_s3('bucket_name', 'code.zip')
subprocess.run(['unzip', '/tmp/code.zip', '-d', '/tmp/'])
#import and run the actual handler, while code.py is the entry point of the project
from code import my_handler
return my_handler(event, context)
Above code is deployed to Lambda function. Its responsibility is to download the zip file, unzip it, import the entry point and then run it. Unzipped size of contents must not exceed 512 MB.