Create AWS Lambda Layers for Python Packages

Saket Jain
6 min readOct 8, 2023

In this blog we will be creating a Lambda Function Layer in Python Without Using EC2 Using CloudShell

To use external Python libraries with AWS Lambda functions that are not included by default, you can create a Lambda Layer that contains the necessary libraries and then attach that layer to your Lambda function. Here’s a step-by-step guide on how to do this:

Creating an AWS Lambda Layer using AWS CloudShell is a straightforward process. Here are the steps to create a Lambda Layer without using EC2, directly from the AWS CloudShell:

Steps that need to be performed:

Step 1: Access AWS CloudShell

Step 2: Prepare Your Libraries

Step 3: Compress Libraries for the Creation of Layer

Step 4: Publish the Layer

Step 5: Attach the Layer to a Lambda Function

Step 6: Save and Test

Step 1: Access AWS CloudShell

  1. Log in to your AWS Management Console.
  2. In the AWS Management Console, open AWS CloudShell. You can usually find it by searching for “CloudShell” in the AWS Console search bar. You can also locate AWS CloudShell at the bottom left of the AWS Management Console by clicking on “CloudShell” for more information Link

Step 2: Prepare Your Libraries

Ensure you have the necessary Python libraries that you want to include in the Lambda Layer. Organize them in a directory structure with a python folder at the root.

mkdir python 


pip3 install <name of the module> -t python/

Step 3: Compress Libraries for the Creation of Layer

  1. In AWS CloudShell, navigate to the directory where your python folder is located. You can use standard Linux commands like cd to navigate to the correct directory.
  2. Create a ZIP archive of the python folder using the following command (replace with your desired layer name):
zip -r python

To download this locally, follow these steps:

  • On the right side, locate the “Actions” button.
  • Under “Actions,” click on “Download file.”
  • Use the absolute path. You can copy the file path from the command-line and paste it below.

Step 4: Publish the Layer

  1. Use the AWS Command Line Interface (CLI) to publish your Lambda Layer using the AWS CloudShell terminal. Replace <layer-name> with your desired layer name and <description> with a brief description:
aws lambda publish-layer-version --layer-name <layer-name> --description "<description>" --zip-file fileb://

This command will publish the Lambda Layer and provide output, including the ARN of the created layer version. Note down this ARN as you will need it to attach the layer to your Lambda functions.


  1. In the Lambda dashboard, click the “Layers” in the left navigation pane.

2. Click the “Create layer” button.

Configure the Layer

  1. Provide the following information for your Lambda Layer:
  • Name: Enter a unique name for your Lambda Layer.
  • Description: Optionally, provide a brief description of the layer.

2. In the “Code entry type” section, select one of the options:

  • Upload a .zip file: Use this option if you have a ZIP archive containing the libraries or code you want to include in the layer. Click the “Upload” button to browse and select the ZIP file.


  • Specify an Amazon S3 object: Use this option if your layer code is stored in an Amazon S3 bucket. Provide the Amazon S3 URL to the layer code package.
  1. Choose a compatible runtime for the layer. Ensure that the runtime matches the runtime of the Lambda functions that will use this layer.
  2. In the Lambda function configuration, scroll down to the “Function code” section.
  3. In the “Code entry type” dropdown, select “Upload a .zip file.”
  4. Click the “Upload” button.
  5. In the file selection dialog, browse your local file system to locate and select the file that you want to upload. This file should contain the code for your Lambda function.
  6. After selecting the file, click the "Open" or "Upload" button (depending on your browser).

Step 5: Attach the Layer to a Lambda Function

To attach the newly created layer to a Lambda function:

  1. Open the AWS Lambda Management Console.
  2. Select the Lambda function you want to add the layer to.
  3. Scroll down to the “Layers” section in the Lambda function configuration.
  4. Click on “Add a layer.”
  5. In the “Choose a layer” dialog, select “Custom layers” and search for your layer by name. Alternatively, you can use the “Specify an ARN” option and copy the ARN.
  6. Select the appropriate version of the layer.
  7. Click “Add” to attach the layer to your Lambda function.

Verify the layer added successfully to the lambda function

Step 6: Save and Test

Save your Lambda function configuration, and your function will now have access to the libraries included in the layer.

After attaching the layer and modifying your Lambda function code, deploy the function and test it to ensure that it works correctly with the imported libraries from the layer.

By following these steps, you can easily create and attach a Lambda Layer using AWS CloudShell without the need for an EC2 instance.

Sample code:

import redis

def lambda_handler(event, context):

return {
'statusCode': 200,

To test a Lambda function in the AWS Lambda console, follow these steps:

1. Navigate to the AWS Lambda Management Console.

2. Select the Lambda function you want to test.

3. In the Lambda function’s configuration, find the “Deploy” or “Test” option and click on it.

4. Follow the on-screen instructions to configure and run a test for your Lambda function. You can provide input data for the test if needed.

5. After the test has been executed, you can view the results and any output generated by your Lambda function.

This allows you to verify that your Lambda function is working as expected.