Your Lambda function is almost never alone: it needs libraries (third-party code) to do its job —connect to a database, process images, call an API—. In this subchapter, we’ll see how to include those dependencies in a Lambda and how Layers let you share them and keep your function lightweight.

The problem: code needs libraries

Imagine a Python Lambda that resizes images. For this, it uses a popular library like Pillow. But that library is not included in Lambda by default: you have to package it along with your code.

Your function needs:
  - your code (handler.py)         ← you write this
  - the Pillow library              ← third-party, must be included
  - maybe other libraries           ← must be included too

If you don’t include the library, the function will fail at runtime with a “module not found” error. So the challenge is: how do I get the libraries into my Lambda?

Option 1: package dependencies with your code

The most direct way is to include the libraries inside the package you upload to Lambda. You install the dependencies in a folder next to your code and upload everything together, usually as a ZIP file:

my-function.zip
 ├── handler.py        ← your code
 ├── Pillow/           ← included library
 └── (other libraries) ← all packaged together

This works, but there’s a drawback: if you have many functions that use the same libraries, you end up repeating those libraries in every package. The code is duplicated, the packages get big, and maintenance is a hassle (if you update a library, you have to rebuild all the packages).

Option 2: Layers

A Layer is a package of libraries or common code that you can share among several functions. Instead of putting the libraries in each function, you put them once in a layer and then attach that layer to all the functions that need it.

        ┌──────── Layer "common-libraries" ────────┐
        │   Pillow, requests, custom utilities...   │
        └───────────────────────────────────────────┘
              ▲            ▲             ▲
              │            │             │
         Function A    Function B     Function C
        (its code)    (its code)     (its code)

Each function includes only its own code, and “borrows” the libraries from the layer.

Analogy: a layer is like the shared pantry in an apartment building. Instead of each neighbor having their own sack of flour and sugar (duplicated), there’s a common pantry where everyone takes what they need. If you need to restock the flour, you do it once for everyone.

Advantages of layers

  • Less duplication: common libraries are in one place, not repeated in every function.
  • Lighter functions: your package only contains your code, so it’s small and uploads/deploys faster.
  • Centralized maintenance: update the library in the layer and all functions benefit.
  • Reuse: you can have a “custom utilities” layer shared by all your teams.

Real-world example: a company has 15 Lambda functions that connect to the same database and use the same internal utilities (logging, validation). They create a company-utilities layer with all that common code. The 15 functions use it. When they improve a utility, they update the layer once and all 15 functions inherit it, without touching each function’s code.

When to use each option?

Situation Recommendation
A simple function, few libraries Package with the code (Option 1)
Several functions with common libraries Use a layer (Option 2)
Heavy libraries that are repeated a lot Use a layer (makes functions lighter)
Shared custom utility code Use a layer

To start: if you have a single function or are learning, packaging libraries with your code is the simplest. Layers make sense when you grow and start repeating the same dependencies in several functions.

A note on size

Lambda has size limits for the code package and layers (we’ll see this in subchapter 14.5). That’s why it’s important not to include libraries you don’t use: the lighter the function, the faster it starts (which connects to “cold starts” in the next subchapter). If your dependencies are very large, there’s also the option to package the Lambda as a container image (think Docker, which we’ll cover in Chapter 17), which allows much larger packages.

What you should remember

  • Your Lambda needs its dependencies (third-party libraries) included; if they’re missing, it fails at runtime.
  • Option 1 — package with the code: you put the libraries in the ZIP along with your function. Simple, but duplicates libraries if you have many functions.
  • Option 2 — layers: you put the libraries or common code once in a layer and share it among several functions. Like a “shared pantry.”
  • Layers reduce duplication, keep functions lightweight, and allow centralized maintenance.
  • To start, packaging with the code is easiest; use layers when you repeat dependencies in several functions.

In the next subchapter, we’ll tackle one of Lambda’s most characteristic topics: cold starts and how to reduce them.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved