preloader
Amazon web services a w s

How to add a Python library as an AWS layer

How to add a Python library as an AWS layer | By Gourav Dhar

newline

Not all libraries are present in the environment provided by AWS lambda (Obviously! It’s not possible). AWS provides a way to tackle this problem. If you want to use a common library (present in pip or a custom library) across various lambda functions, layers are the way to do it.

As of today, lambda does not provide requests. It is a commonly used library in python. I will take this as an example and create a layer for it in AWS.

  1. Create a folder named python
pip install requests -t .

newline

The above command will install the python packages and its dependencies in the current folder, which is python.

The contents of the folder look like this to me

Image

newline

  1. Zip the folder

newline

Image

  1. Open the layers tab in AWS and click on the button Create Layer.

newline

Image

newline

  1. Under name enter the desired name. Click on upload a zip file and upload the file you had zipped earlier. Select the appropriate runtimes and you are done.

newline

Image

newline

Congrats!!! Your AWS layer is ready to be used with lambda functions.