AWS Lambda Deployment Packaging Options
Describe Lambda deployment packaging options
AWS Lambda Deployment Packaging Options
When deploying code to AWS Lambda, developers must choose a packaging format that aligns with their application size, dependency complexity, and existing CI/CD workflows. This guide covers the two primary methods: Zip file archives and Container images.
Learning Objectives
By the end of this guide, you should be able to:
- Differentiate between .zip file archives and OCI-compliant container images.
- Identify the size limitations and storage requirements for each packaging type.
- Explain how Lambda Layers optimize .zip deployments.
- Select the appropriate packaging method based on technical requirements.
Key Terms & Glossary
- Deployment Package: A bundle containing your function code and its dependencies.
- Zip File Archive: The traditional Lambda packaging format where code and libraries are compressed into a single .zip file.
- Container Image: A package formatted as an OCI-compliant image (e.g., Docker) stored in Amazon ECR.
- Lambda Layer: A distribution mechanism for libraries, custom runtimes, and other function dependencies.
- Amazon ECR: Elastic Container Registry; the required storage service for Lambda container images.
- Runtime Interface Client (RIC): A component required in custom container images to communicate with the Lambda Runtime API.
The "Big Idea"
AWS Lambda is "serverless," but the underlying infrastructure still needs a structured way to receive and execute your code. Think of packaging as choosing the shipping container for your application. If your app is small and uses standard libraries, a Zip file is like a standard parcel. If your app is massive (like a Machine Learning model) or relies on complex system-level dependencies, a Container Image is the heavy-duty shipping crate that ensures everything arrives and runs exactly as it did on your local machine.
Formula / Concept Box
| Feature | .zip File Archive | Container Image |
|---|---|---|
| Max Size | 50 MB (zipped direct), 250 MB (unzipped) | 10 GB |
| Storage Service | Amazon S3 (internal) | Amazon ECR |
| Dependency Reuse | Use Lambda Layers | Use Multi-stage Docker Builds |
| Best For | Simple scripts, rapid iteration | Large apps, ML models, Docker-centric teams |
| Tooling | Console, CLI, SAM, CDK | Docker, Buildah, ECR CLI, SAM |
Hierarchical Outline
- I. .zip File Archives
- Standard Method: Most common for simple logic.
- Structure: Code at the root, dependencies in folders.
- Lambda Layers:
- Promotes code reuse across functions.
- Up to 5 layers per function.
- II. Container Images
- OCI Compliance: Must follow Open Container Initiative standards.
- Requirements: Must include a Runtime Interface Client (RIC).
- Scalability: Allows for large binaries (up to 10GB).
- III. Choosing a Strategy
- Size Constraint: If > 250MB (unzipped), use Container Images.
- Environment Control: If specific OS libraries are needed, use Containers.
Visual Anchors
Packaging Decision Flow
Lambda Layer Architecture
\begin{tikzpicture}[node distance=2cm, every node/.style={fill=white, font=\small}] \draw[thick, fill=blue!10] (-2,-0.5) rectangle (6,4); \node at (2, 3.5) {\textbf{AWS Lambda Environment}};
% Function Logic
\node[draw, rectangle, fill=green!20] (func) at (0,2) {Function Code};
% Layers
\node[draw, rectangle, fill=orange!20] (layer1) at (4,2.5) {Layer 1: NumPy};
\node[draw, rectangle, fill=orange!20] (layer2) at (4,1.5) {Layer 2: Custom Lib};
% Arrows
\draw[->, thick] (layer1) -- (func);
\draw[->, thick] (layer2) -- (func);
\node[below] at (2,-0.5) {Layers are extracted to /opt/ during execution};\end{tikzpicture}
Definition-Example Pairs
- Lambda Layer: A way to pull in code from a separate package into your function at runtime.
- Example: A company creates a "SecurityUtils" layer containing standard encryption logic used by 50 different Lambda functions.
- Runtime Interface Client (RIC): An adapter that allows a containerized application to communicate with the AWS Lambda service.
- Example: You use an Alpine Linux base image and must install the
aws-lambda-ricpackage vianpmorpipto make it Lambda-compatible.
- Example: You use an Alpine Linux base image and must install the
- Unzipped Size Limit: The total size of the function code plus all its layers once decompressed.
- Example: If your .zip is 40MB but expands to 260MB, the deployment will fail because it exceeds the 250MB limit.
Worked Examples
Scenario 1: Small Python Web Scraper
- Need: A script using
requestsandbeautifulsoup4. - Solution: .zip file archive.
- Steps:
- Install dependencies locally:
pip install requests -t . - Zip the folder contents:
zip -r function.zip . - Upload via AWS CLI:
aws lambda update-function-code --function-name MyScraper --zip-file fileb://function.zip.
- Install dependencies locally:
Scenario 2: Image Processing with OpenCV
- Need: Large binary libraries (
OpenCV) and a 400MB pre-trained model. - Solution: Container Image.
- Steps:
- Create a
Dockerfileusing the AWS Lambda Python base image. COPYthe model and code into the image.- Build and push to ECR:
docker push <account>.dkr.ecr.<region>.amazonaws.com/my-lambda-image:latest. - Point the Lambda function to the ECR Image URI.
- Create a
Checkpoint Questions
- What is the maximum size for a Lambda function package using the Container Image format?
- True or False: Lambda Layers can be used with Container Image deployments.
- Where is the unzipped code and layer content located within the Lambda execution environment filesystem?
- Which AWS service is mandatory for storing Lambda-compatible container images?
▶Click to see answers
- 10 GB.
- False (Layers are only for .zip archives; containers use Docker layers).
- In the
/optdirectory (for layers) and/var/task(for code). - Amazon Elastic Container Registry (ECR).