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

  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 

Example

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 my-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

  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://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

  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.

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.
  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 zip.zip file that you want to upload. This file should contain the code for your Lambda function.
  6. 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:

  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.

Output:

Congrats!

--

--

๐’๐š๐ค๐ž๐ญ ๐‰๐š๐ข๐ง

๐ƒ๐ž๐ฏ๐Ž๐ฉ๐ฌ/๐’๐‘๐„/๐‚๐ฅ๐จ๐ฎ๐ /๐ˆ๐ง๐Ÿ๐ซ๐š๐ฌ๐ญ๐ซ๐ฎ๐œ๐ญ๐ฎ๐ซ๐ž /๐’๐ฒ๐ฌ๐ญ๐ž๐ฆ ๐„๐ง๐ ๐ข๐ง๐ž๐ž๐ซ