How Use Python PIP Module With AWS Lambda

yehan anushka
CloudTricks
Published in
4 min readFeb 29, 2020

What is AWS lambda

Lambda is a compute service which is provided by AWS to run your code without provisioning server. You can run your code only when you needed. Lambda support for fallowing languages.
1. .NET Core
2. Go
3. Java
4. Python
5. Node.js
6. Ruby

Today we are going to discuss PIP module installation issue when we are facing implement lambda with python.

Problem
When we developing using python we need to import required modules. Modules which are required can be install using pip when we developing inside server. But we can’t install PIP module when we are using lambda.

Resolution
We can create a python virtual environment, do the development inside the it & upload to lambda. Now let see how can we do that.

Step 01

Installing Virtualenv using pip

We need to install virtualenv on local pc. Use fallowing command for this, it will install python virtualenv on your local pc.

$ pip install virtualenv

Create new folder, here I’m going to create folder call sample-lambda.
$ mkdir sample-lambda
Now create the virtual environment inside above created folder.
$ virtualenv sample-lambda/
Above command will create the isolated python environment for your development.

Create python virtual environment

When we list the sample-lambda directory now you can see fallowing folder architecture.

virtual environment directory architecture

Activate Created Virtual environment
$ source sample-lambda/bin/activate
Use above command to activate new the virtual environment, then go inside to sample-lambda directory.

Activated Virtual environment

Step 02

Implement python code inside the virtual environment

Now we can implement python script inside sample-lambda directory. I’m going to implement simple python script for run curl command using python.

I’m using fallowing python script to call to the URL

In here I have used request pip module. when I run this on local fallowing errors are occurred.

For fixing above issue we need to install request module inside over virtual environment. Use fallowing command for it.
# pip install requests

Install pip module request

Now issue is resolved and can get the expected output

expected output

Ready python environment upload to lambda
Deactivate virtual environment. Use deactivate command for it.
# deactivate
Go inside /sample-lambda/lib/python2.7/site-packages/
# cd /sample-lambda/lib/python2.7/site-packages/
Then zip the directory content to sample-lambda directory.
# zip -r9 ${OLDPWD}/function.zip

Zip virtual environment

This will zip site-packages entire directory to function.zip file.

Go back to sample-lambda directory and append created sample.py to zip created by above step.
# zip -g function.zip sample.py

Now we have created the zipped python environment to upload to lambda, then continue with lambda function creation.

Step 03

Creating Lambda Function

Login to your AWS account and navigate to Services -> Compute -> lambda then go to functions and create function . It will show fallowing window

lambda creation window

Fill the requested data
Function name: Name of the lambda function.
Runtime: Chose python 2.7
Execution role: If you had existing role you can attach that, if you done have choose Create a new role with basic Lambda permissions
Then select create function.

After creating this lambda function open Code entry type drop-down & select upload a .zip file , upload created zip file in above step 02 & save the changers.

After uploading zip file you can see fallowing & change the sample.py as fallows.

import requests

def main(event, context):
url = ‘
https://wpstest98.000webhostapp.com/index.php'
#payload = open(“request.json”)
headers = {‘content-type’: ‘application/json’, ‘Accept-Charset’: ‘UTF-8’}
r = requests.post(url,headers=headers)
return r.content

Now configure empty test event, save the changers and test your lambda function that will show bellow output.

--

--

yehan anushka
CloudTricks

DevOps Engineer at Novigi Pty Ltd | RHCSA | AWSCSAA