AWS Lambda is a powerful serverless compute service, but managing external dependencies can sometimes be a headache. If your function relies on libraries like requests or pandas that aren't included in the standard AWS runtime, you need a way to bundle them.

Lambda Layers enable you to package these dependencies separately from your application code. In this guide, we will use AWS CloudShell to download, package, and upload Python libraries to S3 for use as a Lambda Layer.

Prerequisites

  • An AWS Account.
  • An S3 bucket to store the layer package.

Step 1: Set Up the Environment in CloudShell

We use CloudShell because it is pre-configured with AWS CLI and Python, making it the perfect clean environment for packaging. Open CloudShell from the top-right of the AWS Console.

First, we create a workspace and a virtual environment to keep things clean:

BASH
mkdir packages
cd packages
python3 -m venv venv
source venv/bin/activate

Step 2: Install Libraries in a Specific Structure

AWS Lambda requires Python layers to be inside a folder named python. If this structure isn't followed, Lambda won't be able to find your libraries.

BASH
mkdir python
cd python
pip install requests -t .
rm -rf *dist-info
cd ..

Note: We remove the dist-info folders to save space, as they aren't strictly necessary for runtime.

Step 3: Zip and Upload to S3

Finally, we zip the python folder and upload it to your S3 bucket using the AWS CLI.

BASH
zip -r my-first-lambda-package.zip python
aws s3 cp my-first-lambda-package.zip s3://your-s3-bucket-name/

You can now go to the AWS Lambda console, create a layer, and point it to the S3 object URL of the zip file you just uploaded.