Sitemap
Towards AWS

Where Cloud Experts, Heroes, Builders, and Developers share their stories, experiences, and solutions.

Create AWS Lambda Layer [Python]

3 min readApr 25, 2024

--

Similar to Python environments, AWS Lambda Layers provide a way to handle external libraries and dependencies for your serverless functions.

Think of a Layer as a zip archive containing libraries. These archives are uploaded to AWS and referenced by your Lambda functions. When a function is invoked, they are accessible just like locally installed packages.

Get Raymond Toh’s stories in your inbox

Join Medium for free to get updates from this writer.

If you are new to Lambda Function, you can visit my Step-by-step guide on [Free Project] AWS Lambda with API Gateway

Create Lambda Layer [Step-by-Step]

  1. Check your environment runtime version.
Press enter or click to view image in full size

2. Go to Google Colab Here. Run the following command in Colab. Remember to update your Runtime from Step 1.

import os
RUNTIME = "python3.10" #UPDATE TO YOUR RUNTIME in Step 1
DIR_PATH = f"python/lib/{RUNTIME}/site-packages"
os.makedirs(DIR_PATH)

3. Verify the folders are created by clicking Refresh Button.

Press enter or click to view image in full size

4. Run the below command. Remember to update the package.

PACKAGE = "pandas" #UPDATE THE PACKAGE HERE
!python3 -m pip install --upgrade {PACKAGE} -t {DIR_PATH}

5. Verify the packages have been installed in the site-packages folder.

Press enter or click to view image in full size

6. Run the below command.

!zip -r {PACKAGE}.zip python

7. Verify that your package has been zipped.

Press enter or click to view image in full size

8. Right Click your package and Download it.

Press enter or click to view image in full size

9. In AWS Lambda, Click on Layer, and Create Layer.

Press enter or click to view image in full size

10. Upload the zip file. Choose the runtime you used in Step 1. Follow the default setting and Create Layer.

Press enter or click to view image in full size

11. Congratulation! You have successfully created a Lambda Layer!

Use Lambda Layer [Step-by-Step]

  1. Go to your Lambda Function. Click on Layers under your Lambda Function.
Press enter or click to view image in full size

2. Click on Add a Layer.

Press enter or click to view image in full size

3. Choose Custom Layers. Pick the Layer you created. The version should be 1. Then Click on Add.

Press enter or click to view image in full size

4. Verify the layer have been added.

Press enter or click to view image in full size

Congratulation! You have added a layer to your function! Do take note that a layer can be reused on other Lambda Functions.

I’ll be sharing articles about the applications I’m creating while exploring free AWS services. Join me to stay updated on my journey!

Past Articles:
AWS Free Tier Zero Budget Setup
[Free Project] AWS Lambda with API Gateway

--

--