In Lambda when we write code in python we need to import some module that is not build in .
As Lambda is serverless service and We dont have access to server where we can do “pip install module”.
In this case we need to create layer for that we have to download required module in path “python/lib/python3.7/site-packages/”
and zip and upload while creation layer. Once layer created we can add layer in our Lambda service.
So lets start doing it.
To run “pip install module” we need server. Even we can use windows local machine to download the module. But sometime module imported in your local windows machine will not work in Lambda.
Because of this reason we are choosing sagemaker to create Layer.
Even you can do the same using EC2 machine. But for this blog I will guide how you can create layer using sagemaker. So lets start:-
Login to AWS console and search for Lambda and sagemaker service .
Open both service in different tab.
Today We will learn to create layer for pandas module.
Do the following in sagemaker notebook.
create python file from home
!mkdir -p build1/python/lib/python3.7/site-packages
then go to path build1/python/lib/python3.7/site-packages then create new python file and run
!pip install pandas -t .
then go to build1 path open python file and run !zip -r -X pandas_3.7.zip python/lib/python3.7/site-packages/*
You will see pandas_3.7.zip file in build1 path.
Download this file.
Go to Lambda function.
Choose Layer then upload pandas_3.7.zip file, provide runtime as python 3.7 then create layer.
Now go to Lambda function add this layer and run your code that having “import pandas as pd”.
This time it wont give error.
Please try and let me know in comment in case of any issue.