How to fix “Unable to import module ‘lambda_function’: No module named ‘lambda_function”

Mariem SOUSSI
2 min readMar 29, 2024

--

When testing a Lambda function written in Python, you may encounter the error ‘Unable to import module ‘lambda_function’: No module named ‘lambda_function’, where ‘lambda_function’ is the name you’ve chosen for your Lambda function.

Why does this issue occur?

The Lambda service cannot find the Handler method of your lambda function to run it.

How to solve this issue?

To resolve this issue, you need to check two things:

  1. Your Folder Structure

When running a Lambda function with a zipped file that has no dependencies, the .py file must be at the root of your .zip file. If it’s not, Lambda won’t be able to find it and execute your code. You can manually zip your code to ensure this or use the correct commands. Here’s an example of the correct commands:

cd your_code_folder
zip -r your_output_zip_filename.zip .

2. The Handler Setting

When configuring the Lambda function, either through an Infrastructure as Code (IAC) tool like Terraform or via the console, ensure that the value of the handler setting is in the format py_file_name.handler_module_name For example, in the case mentioned, the handler name should be lambda_function.lambda_handler

Hope you find this information helpful 😊!

For More details

Creating a .zip deployment package for a lambda function

Lambda function handler in Python

--

--