- SOTA Embedding Retrieval: Gemini + pgvector for Production Chat
- A Review of Agentic Design Patterns
- Model Context Protocol (MCP) and MCP Servers in LLM Agent Systems
- Building AI Agents for Automated Multi-Format Content: From News to Podcasts
- Rediscovering Cursor
- GraphRAG > Traditional Vector RAG
- Cultural Bias in LLMs
- Mapping out the AI Landscape with Topic Modelling
- Sustainable Cloud Computing: Carbon-Aware AI
- Defensive Technology for the Next Decade of AI
- Situational Awareness: The Decade Ahead
- Mechanistic Interpretability: A Survey
- Why I Left Ubuntu
- Multi-Agent Collaboration
- Embeddings and Vector Databases: Enhancing Retrieval Systems
- Building an Automated Newsletter-to-Summary Pipeline with OpenAI: Zapier AI Actions vs AWS SES & Lambda
- Local AI Image Generation
- MLOps: Deploying a Distributed Ray Python Server with Kubernetes, EKS & KubeRay
- Making the Switch to Linux for Development: A Developer's Experience
- Scaling Options Pricing with Ray
- The Async Worker Pool
- Browser Fingerprinting: Introducing My First NPM Package
- Reading Data from @socket.io/redis-emitter without Using a Socket.io Client
- Socket.io Middleware for Redux Store Integration
- Sharing TypeScript Code Between Microservices: A Guide Using Git Submodules
- Efficient Dataset Storage: Beyond CSVs
- Embracing Next.js 13: Why I switched from Plain React
- Deploy & Scale Socket.io Containers in ECS with Elasticache
- Implementing TOTP Authentication in Python using PyOTP
- ›Simplifying Lambda Layer ARNs and Creating Custom Layers in AWS
- TimeScaleDB Deployment: Docker Containers and EC2 Setup
- How to SSH into an EC2 Instance Using PuTTY
Simplifying Lambda Layer ARNs and Creating Custom Layers in AWS
AWS Lambda (λ) Layers play a crucial role in simplifying the deployment process for Lambda functions. They provide an easy way to collect and manage Python packages as AWS Lambda Layers, streamlining the development workflow. In this article, we'll explore how to use pre-existing Lambda Layers and create custom layers from scratch using Flask as an example.
Keith's Layers for Easy Deployment
If you're looking for readily available Lambda Layers, Keith's Layers1 is a fantastic resource. These layers make it incredibly convenient to deploy Python packages for AWS Lambda.
You can quickly access a collection of Python packages as AWS Lambda Layers, making the deployment process much more straightforward. The repository provides pre-built layers for popular packages, eliminating the need to create common layers from scratch.
Creating Your Own Custom Layers
Creating a custom Lambda Layer tailored to your specific needs is a powerful approach. Let's walk through the four main steps to create a Lambda Layer with Flask:
Setting up the Local Directory for the Layer
To begin, create a local directory for the Lambda Layer. In this example, we'll use Flask. Execute the following commands:
mkdir flask-layer
cd flask-layer
mkdir -pv python/lib/python3.8/site-packages
This creates a directory named flask-layer
with a subdirectory python/lib/python3.8/site-packages
, where we'll install our Python packages.
Adding the Requirements File
Next, create a requirements.txt
file in the root of the flask-layer
folder, specifying the required packages. For instance, for Flask, the requirements.txt
may contain:
flask==1.1.1
The file structure should now look like this:
flask_layer
├── python
│ └── lib
│ └── python3.8
│ └── site-packages
└── requirements.txt
Installing Requirements with Docker
To install the requirements in the local directory using Docker, run the following command in PowerShell when you are in the root directory:
docker run -v ${pwd}:/var/task "amazon/aws-sam-cli-build-image-python3.8" /bin/sh -c "pip install -r requirements.txt -t python/lib/python3.8/site-packages; exit"
This command populates the site-packages
directory you created earlier with all the necessary dependencies.
Uploading Your Custom Layer to AWS
AWS requires all the layer code to be in a zip archive, so we need to zip everything in the python directory:
zip -r flask-layer.zip python
Now the layer can be uploaded to AWS using the AWS CLI. You need to provide a few parameters in this step:
layer-name
- the name you want to give your layerdescription
- to briefly summarize the layerzip-file
- the path to the zip archive you created in the previous stepcompatible-runtimes
- details the Python versions your layer is compatible with
Use the following terminal command to publish your custom layer to AWS:
aws lambda publish-layer-version \
--layer-name "flask-layer" \
--description "Lambda Layer for Flask 1.1.1" \
--zip-file "fileb://flask-layer.zip" \
--compatible-runtimes "python3.8"
Conclusion
With these steps, you can create a custom Lambda Layer tailored to your application's needs, enhancing the efficiency and maintainability of your AWS Lambda functions. Whether you use pre-existing layers from Keith's repository or build your own, Lambda Layers provide a clean separation of dependencies from your function code.
References
Footnotes
-
Keith Rozario: Klayers - Python Packages as AWS Lambda Layers, GitHub Repository ↩