Tired of "No module found" errors in your Lambda functions? Architecture mismatches got you down? Let me show you how to build Lambda layers the right way! 💪
The Problem
Building Lambda layers locally often leads to compatibility issues, especially when your development machine doesn't match your Lambda's architecture (x86_64 vs ARM64).
Our use case and why this matters
For this example lets study a bit the Langfuse library for python if you are a Windows user and you try to generate this in your local independent if is a virtual environment or standard, once you generate the zip this will crash once you upload it as a layer and link it with your lambda due the pydantic_core.
This approach ensures that compiled modules (like pydantic_core) are built for the exact architecture your Lambda uses, eliminating those frustrating "no module found" errors.
Lets avoid the arch mistmatches forever 🫧💗✨
The Solution
I created this simple Docker Compose setup that builds Lambda layers with the exact same environment as AWS Lambda:
AWS Official images link : (https://gallery.ecr.aws/sam/)
version: '3.8'
services:
lambda-builder:
image: public.ecr.aws/sam/build-python3.12:latest-arm64
volumes:
- .:/var/task
command: >
bash -c "
echo 'Building Lambda dependencies for ARM64...' &&
# Install dependencies directly in the root directory where Lambda will look for them
pip install langfuse -t /var/task/package/ &&
cd /var/task/package &&
zip -r /var/task/deployment-package.zip . &&
cd /var/task &&
if [ -f 'lambda_function.py' ]; then
zip -g /var/task/deployment-package.zip lambda_function.py;
else
echo 'Warning: lambda_function.py not found in the current directory';
fi &&
if [ -f '.env' ]; then
zip -g /var/task/deployment-package.zip .env;
fi &&
echo 'Deployment package created at: /var/task/deployment-package.zip' &&
ls -la /var/task/deployment-package.zip
"
How to Use It
1️⃣ Save the Docker Compose file in your project folder
Create a new directory with the above docker-compose.yml file
mkdir lambda-layers-generator
2️⃣ Run the build process
docker-compose up
3️⃣ Upload the generated deployment package
The output file 'deployment-package.zip' will be in your project directory, ready to upload to AWS Lambda!
What's Actually Happening?
Uses AWS's official Lambda container image (identical runtime environment)
Installs dependencies directly at the root level (where Lambda expects them)
Creates a properly structured ZIP package with all necessary files
Works for both ARM64 and x86_64 (just change the image tag)
No more "but it works on my machine" moments! 😅
And That's It!
💬 Got questions or improvements? Drop a comment below!
🔥 If this helped you, give it a ❤️ and share it with other Developers!
🔗 Connect With Me!
💼 LinkedIn: CodexMaker
📂 GitHub: CodexMaker
🎥 YouTube: CodexMaker
Have questions or improvements? Drop a comment below! 🚀🔥