How to create a lambda using Python with dependencies

Posted on Sun 03 May 2020

python aws lambda modules

When you have a Python script that uses modules, which are not included in the Python Standard Library, and want to run it as a lambda in AWS, you have two options. First one would be to create a AWS Lambda Layer that contains all the packages and then is connected to the Lambda, and the second would be to zip together the Lambda and the modules, creating a package that can then be uploaded and run. This article will cover the second option.

The Code

As an example I will be creating a small script that uses the Python library Requests. We will be pulling a random dad joke from icahasdadjoke.

In the terminal, create a new folder, create a new virtual environment, activate it and install requests.

mkdir dad-jokes-lambda
cd dad-jokes-lambda
python3 -m venv venv
source venv/bin/activate
pip install requests

We can now create the script:

touch lambda_function.py
vim lambda_function.py

The code will look something like the following:

import json
import requests

url = 'https://icanhazdadjoke.com'

def lambda_handler(event, context):
    r = requests.get(url, headers={"Accept": "application/json"})
    result = json.loads(r.text)
    return result['joke']

The Lambda

To create the lambda, login into the AWS console and go to Services -> Lambda. - Create function - Author from scratch - Fill in the function name (randomDadJoke) - Runtime - Python 3.8 - Create function

We also need to create a test event so we can trigger the lambda manually. You can do that by clicking on the dropdown (Select a test event) -> Configure test events. - Give it an Event Name (ex: Run) - Inputs don't matter so you can just leave it as is or delete those keys - Create

Now, you could paste the code in the text editor in your browser and save, but you will notice that if you try running the lambda , by clicking Test, it will fail, because it cannot find the requests module. We are going to fix that next.

The Bundle

Back in the terminal, in the project folder, we will create a zip file with the dependencies and then add the lambda code to that zip file.

cd venv/lib/python3.8/site-packages
zip -r9 ${OLDPWD}/function.zip .
cd $OLDPWD
zip -g function.zip lambda_function.py

All together now

Back in the AWS console, on the lambda screen, in the Function code section, click on the dropdown for Code entry type and select Upload a .zip file. - Upload - Select the function.zip file created - Open - Save

Optionals

You will notice that you can no longer see the code editor, but instead you see a message telling you why. Every time you want to change the code for that lambda, you will have to update it on your machine, update the zip file with the new code and upload it again.

zip -u function.zip lambda_function.py

Test

You can now click on the Test button on top and the lambda will run, rewarding you with a dad joke. Have fun!