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
- Log in to your AWS Management Console.
- 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.
pip3 install <name of the module> -t python/
Step 3: Compress Libraries for the Creation of Layer
- In AWS CloudShell, navigate to the directory where your
pythonfolder is located. You can use standard Linux commands like
cdto navigate to the correct directory.
- Create a ZIP archive of the
pythonfolder using the following command (replace
my-layer.zipwith your desired layer name):
zip -r my-layer.zip 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
- 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://my-layer.zip
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.
- In the Lambda dashboard, click the “Layers” in the left navigation pane.
2. Click the “Create layer” button.
Configure the Layer
- 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.
- Choose a compatible runtime for the layer. Ensure that the runtime matches the runtime of the Lambda functions that will use this layer.
- In the Lambda function configuration, scroll down to the “Function code” section.
- In the “Code entry type” dropdown, select “Upload a .zip file.”
- Click the “Upload” button.
- In the file selection dialog, browse your local file system to locate and select the
zip.zipfile that you want to upload. This file should contain the code for your Lambda function.
- After selecting the
zip.zipfile, 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:
- Open the AWS Lambda Management Console.
- Select the Lambda function you want to add the layer to.
- Scroll down to the “Layers” section in the Lambda function configuration.
- Click on “Add a layer.”
- 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.
- Select the appropriate version of the layer.
- 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.
def lambda_handler(event, context):
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.