AWS Lambda layers allow you to package libraries and dependencies that can be shared across multiple Lambda functions. This can help reduce deployment package sizes and make it easier to manage common dependencies across your serverless applications. In this blog post, we'll walk through how to create a Lambda layer for the LangChain library, which can be used for building applications with large language models.
First, we'll need to pull the AWS SAM build image for Python 3.10 from the public Amazon ECR registry. Open your terminal and run:
sudo docker pull public.ecr.aws/sam/build-python3.10:1.110.0-20240222205900
NOTE: Ensure docker is started.
Next, create a new directory to store the files for your layer:
mkdir task
Now run the pulled Docker image with your working directory mounted inside the container:
sudo docker run -it -v $(pwd):/var/task public.ecr.aws/sam/build-python3.10:1.110.0-20240222205900
This will start an interactive terminal session inside the container with your task
directory mounted to /var/task
.
Within the container, install LangChain and any other required Python dependencies to the python
subdirectory:
pip install langchain -t ./python
Or use a requirements.txt file:
pip install -r requirements.txt -t ./python
Once the dependencies are installed, create a Zip archive containing the python
directory:
zip -r python.zip ./python
This python.zip
file will contain all the installed dependencies and can be used to create the Lambda layer.
Exit the Docker container and head to the AWS Lambda console. Click on "Layers" and then "Create layer". Provide a name and description for your layer, and upload the python.zip
file you just created.
After creating the layer, copy its ARN. Then, go to your Lambda function, scroll down to "Layers", click "Add a layer", and paste in the ARN you copied earlier. Make sure to select the latest runtime that matches the Python version you used (Python 3.10 in this case).
With the LangChain layer added, you should now be able to import and use LangChain in your Lambda function code. Test it out by invoking your function with a sample event payload containing a YouTube video URL.
That's it! You've now created a Lambda layer for LangChain that can be easily shared and used across multiple Lambda functions within your AWS account.