top of page
Search

Solving AWS Lambda Deployment Size Limit with Serverless Framework and Layers

  • Writer: Avishag Sahar
    Avishag Sahar
  • Jan 27
  • 2 min read

The Problem: Deployment Size Limit

AWS Lambda enforces a deployment package size limit:

  • 50 MB for zipped packages.

  • 250 MB for unzipped packages, including all dependencies.

In my case, my Lambda function required heavy Python libraries such as pandas, resulting in a package size of 60.7 MB (zipped) and exceeding the limit.


Error Example:



In this case, the requirements.txt file included heavy dependencies like pandas, resulting in an oversized unzipped package.



The Solution: Using AWS Lambda Layers


To resolve this, I used the Serverless Framework's built-in support for layers to separate the dependencies from the main Lambda function. This approach split the deployment package into:

  • The Lambda function ZIP file: Containing only the application logic.

  • A layer ZIP file: Containing all the dependencies defined in requirements.txt.

What Are Layers?

Lambda layers are a way to include common code and dependencies that can be shared across multiple Lambda functions. This reduces duplication and keeps the Lambda deployment package small.



Serverless Configuration

Here’s the relevant configuration from my serverless.yml file that helped solve the issue:

Key Changes

  • Remove unused dependencies to reduce size (could also move them to dev-requirements.txt file

  • Enabled layers for dependencies by setting layer: true under pythonRequirements.

  • Slimmed the dependencies by using slim: true and caching options to optimize the layer.

  • Specified patterns to include only essential files in the deployment package.

  • Link the layer to the lambda function to associate the function with the relevant layer.



Deployment Results

After splitting the deployment package into two parts, the deployment was successful:



By using the Serverless Framework's support for layers and fine-tuning the serverless.yml configuration, I successfully deployed my Lambda function without hitting the size limit.

If you’re facing similar challenges, this approach can save you from deployment headaches.

 
 
 

Comments


Post: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2021 by Avishag Sahar. Proudly created with Wix.com

bottom of page