Create AWS Lambda Layers for Python Packages
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
- 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.
mkdir python
Example
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
python
folder is located. You can use standard Linux commands likecd
to navigate to the correct directory. - Create a ZIP archive of the
python
folder using the following command (replacemy-layer.zip
with 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.
OR
- 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.
OR
- 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.zip
file that you want to upload. This file should contain the code for your Lambda function. - After selecting the
zip.zip
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:
- 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.
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.
Output:
Congrats!