Lambda Layers and Dependencies

Learn how to manage AWS Lambda dependencies, evolving from direct bundling in deployment packages to using Lambda Layers for improved efficiency and reusability.

This lesson introduces the evolution of dependency management in AWS Lambda functions. We begin by exploring the traditional approach of bundling dependencies directly within the deployment package, identifying its limitations and challenges. We then transition to AWS Lambda layers, a powerful mechanism for extracting shared code, improving modularity, and optimizing serverless deployments.

The need for external libraries in Lambda

In real-world development, Lambda functions often require third-party libraries that are not included in the minimal AWS Lambda runtime. While AWS provides some built-in libraries like boto3 for Python, many applications rely on packages such as requests, numpy, lodash, or uuid to function properly.

To accommodate these libraries, one approach is to bundle dependencies directly with the function code in a single ZIP deployment package. Alternatively, we can package the function code and dependencies as a container image. Both methods are deployment package approaches. The difference lies in how the code and its dependencies are structured and deployed. Let’s walk through the ZIP-based approach first.

Packaging dependencies with our Lambda function using a ZIP file

When we need additional packages not available in the standard Lambda runtime, we install them locally and upload them in the function as a .zip file. This method is straightforward for small-scale deployments.

Here is what the directory structure typically looks like:

my-function/
├── lambda_function.py # Code file
├── requests/ # Installed dependency
Lambda function deployment package structure

To implement this setup, we can follow these steps:

mkdir my-function && cd my-function
pip install requests -t .
zip -r function.zip .
Create the deployment package

The function can then be deployed using the AWS CLI:

aws lambda create-function \\
--function-name myFunction \\
--runtime python3.11 \\
--handler lambda_function.lambda_handler \\
--zip-file fileb://function.zip \\
--role arn:aws:iam::<Account-ID>:role/lambda-execution-role
Create a Lambda function with a ZIP file deployment package

Packaging dependencies with our Lambda function using a container image

The container image approach is an alternative method to deploy AWS Lambda functions. Instead of zipping the source code and dependencies, we build a Docker image and push it to Amazon Elastic Container Registry (Amazon ECR). This method is particularly useful when the dependencies are large or need native binaries that are hard to package using the ZIP method. ...