How to install custom packages to AWS Lambda

Dhiraj Rai
3 min readApr 7, 2021

Lambda is a very useful service offered by AWS, but it does contain all the python packages, that we normally use while python scripting.

In this blog post I will show you two methods to install desired python package to AWS Lambda

Method # 1:

Step 1: Login to the AWS console and go to AWS Cloud9

Step 2: Once into Cloud9, click on “Create Environment”

Step 3: Give any name to your environment and click “Next Step” >> “Next Step” >> “Create environment” (Keep everything as default)

(It would take few minutes for the environment to initialise)

Step 4: Once the environment is created. Enter the following code line by line

mkdir folder
cd folder
virtualenv v-env
source ./v-env/bin/activate
pip install pandas
deactivate

Step 5: Type the following command line by line

mkdir python
cd python
cp -r ../v-env/lib64/python3.7/site-packages/* .
cd ..
zip -r panda_layer.zip python
aws lambda publish-layer-version --layer-name pandas --zip-file fileb://panda_layer.zip --compatible-runtimes python3.7

if you get an error in the third line “No such directory” then

(1) please check the python version

(2) please check the path to site-packages, by manually going into each folder till you reach site-packages

Once done, you must see the following json on the command line.

Step 6:

Just copy the ARN: arn:aws:lambda:us-east-1:158019657495:layer:pandas:2

Step 7: Go to AWS Lambda >> Create function >> keep all as default >> choose run time python 3.7 (as we have used python 3.7 in step 5)

(please ensure the region in which the Lambda is create is the same as the region mentioned in the arn) e.g. us-east-1

Step 8: Once the function is created go to layers >> Add layer

Step 9: Click on Add layer

Step 10: Then choose “Specify an ARN” option, and copy paste the above mentioned ARN

Step 11: Scroll down to “Code Source” >> import pandas >> click “Deploy” >> click “Test”

You should see the following

This shows that the pandas library is successfully run

Steps 1–6 Can also be done through your local command prompt, if you have installed AWS cli

Steps 1–6 Can also be done through any EC2 instance, if you have installed AWS cli

--

--