This project creates an AWS Lambda Layer containing LangChain and related dependencies. Lambda Layers let you include libraries in your Lambda functions without bundling them with your function code.
This project automates building a Lambda Layer for LangChain using Docker to ensure compatibility with the Lambda runtime environment. The generated layer can be directly uploaded to AWS Lambda.
The project uses Docker to:
- Create a Lambda-compatible environment
- Install Python packages into the correct directory structure
- Package them into a ZIP file ready for Lambda
.
├── Dockerfile # Defines the Lambda layer build process
├── requirements.txt # Python dependencies for the layer
├── create_layer.sh # Script to build and extract the layer
└── readme.md # Documentation
- Docker installed and running
- AWS CLI (for uploading the layer) - Optional
- Basic shell environment
-
Clone the repository:
git clone https://github.com/derevya/aws-langchain-lambda-layer.git cd aws-langchain-lambda-layer
-
Customize dependencies (optional): Edit
requirements.txt
to add or remove packages as needed. -
Run the build script:
chmod +x create_layer.sh ./create_layer.sh
-
Upload to AWS Lambda:
aws lambda publish-layer-version \ --layer-name langchain-layer \ --description "LangChain dependencies" \ --zip-file fileb://langchain_lambda_layer.zip \ --compatible-runtimes python3.12
Alternative: Upload via AWS Web Console:
-
Upload the ZIP file to an S3 bucket:
aws s3 cp langchain_lambda_layer.zip s3://your-bucket-name/
-
In the AWS Lambda Console:
- Go to "Layers" in the left navigation
- Click "Create layer"
- Enter a name for your layer (e.g., "langchain-layer")
- Under "Code entry type", select "Amazon S3 link"
- Paste the S3 URL of your ZIP file (max 50MB allowed)
- Select compatible runtime(s) (e.g., Python 3.12)
- Click "Create" to publish the layer
-
Lambda layers follow a specific directory structure:
python/
└── lib/
└── python3.12/
└── site-packages/
├── langchain_community/
├── langchain_core/
└── ...
This structure enables AWS Lambda to automatically include these packages in your function's Python path.
To add custom dependencies, simply edit the requirements.txt
file, adding one package per line:
# Add packages like this:
package-name==version
another-package>=minimum.version
To target a different Python runtime:
- Change the base image in the Dockerfile
- Update the installation path to match your Python version
- Layer size limits: AWS Lambda layers have a max unzipped size of 250 MB
- Dependencies compatibility: Some packages with native code might require additional configuration
- Permission errors: Ensure the Docker daemon has sufficient permissions to build and run containers
- Memory limitations: When using LangChain with Lambda, you'll likely need to increase the function's memory from the default 128MB to at least 1024MB for better performance
Contributions are welcome! Please feel free to submit a Pull Request.