Sitemap
Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

AWS Lambda with Python Tutorial

2 min readDec 26, 2023

--

High level Guide to Writing and Running AWS Lambda with Python.

Press enter or click to view image in full size
Photo by Artturi Jalli on Unsplash

Two Options to Write a Lambda Function:

  1. Write the Function Inside AWS Editor

This Option can only be used when when your lambda doesn’t need any libraries apart from the default libraries provided by AWS.

Some important default ones are boto3, requests , json. However Boto3 is only available with Python runtime of 3.7 and not any other as of December 2023.

2. Write the lambda Function in your code editor, zip it and upload the zip to AWS Lambda upload.

This option is the only choice when your code uses external libraries. In my case I was using pymongo to query MongoDB database, hence I went with this option. Its a bit longer process then the first option.

Get Hatim Zahid’s stories in your inbox

Join Medium for free to get updates from this writer.

Details About Option 2 :

  1. Create a file called lambda_function.py and write the function which will receive the events/ which will be called , name it as

def lambda_handler(event, context):

Note : By default AWS calls the lambda_handler as lambda_function.lambda_handler. This configuration can be overwritten, but i wont cover that in this tutorial.

  1. Download the package you want to use with pip, specifying the location ; a new folder which is at the root of lambda_function.py. Name it ‘dependencies’ (not mandatory, can be named as you like).
  2. zip the dependencies folder and then zip the top most folder, the folder in which both dependencies and lambda_function.py are living. At this stage you have a nested zip folder structure.
  3. Upload the top-most folder in AWS Lambda.

If there are errors of lambda_function.lambda_handler cannot be found, Use the AWS Editor Window to move the python file and all the other packages file from your uploaded folder to the root of the lambda function hierarchy. This can be simply done by dragging and dropping all the files.
I was getting the same error and after testing for 1 hour i found that the issue was the structure of the folders.
I had created a lambda called “era_syncing” and the uploaded my zip folder called “syncing_with_mongodb” to the lambda panel. Hence my hierarchy was syncing-> syncing_with_mongodb -> python file + python packages.
This created errors because I nested the python file and packages but they should be at the root of the lambda. I moved the files one hierarchy above and then deleted the folder of “syncing_with_mongodb”.

After solving the error, the new hierarchy was as follows:

Correct Folder Hierarchy

--

--

Hatim Zahid
Hatim Zahid

Written by Hatim Zahid

An engineer who loves to travel, loves animals and loves philosophical conversations !! Ready anytime for a chat. https://medium.com/subscribe/@hatim.zahid

Dev Genius
Dev Genius

Published in Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development