Skip to content

Instantly share code, notes, and snippets.

@labeveryday
Created March 26, 2024 01:47
Show Gist options
  • Save labeveryday/d725d88327e1452a7cbacec5a25e6a2d to your computer and use it in GitHub Desktop.
Save labeveryday/d725d88327e1452a7cbacec5a25e6a2d to your computer and use it in GitHub Desktop.
Here is a blog post on how to create a Lambda layer for LangChain

How to Create a Lambda Layer for LangChain

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.

Step 1: Pull the AWS SAM Build Image

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.

Step 2: Create a Working Directory

Next, create a new directory to store the files for your layer:

mkdir task

Step 3: Run the Container with the Working Directory Mounted

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.

Step 4: Install LangChain and Dependencies

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

Step 5: Create Lambda Layer Zip

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.

Step 6: 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.

Step 7: Add Layer to Lambda Function

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).

Step 8: Test Your Lambda Function

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment