AWS Lambda Python function: import module error: module not found

Viewed 1350

This has been already asked a number of times, but I have tried everything suggested plus more, and nothing seems to work.

My setup: an application on Lambda, with python functions and deployed via CloudPipeline. This is the full error I get (and all I can see in the logs):

   {
  "errorMessage": "Unable to import module 'lambda_functions.function_one': No module named 'lambda_functions'",
  "errorType": "Runtime.ImportModuleError"
}

lambda_functions is a directory, and function_one is the name of the python file with the handler in it. The full function invocation path in my template.yml is: lambda_functions.function_one.lambda_handler. I do have a __init__.py in that dir.

I installed the AWS SAM tools and I can invoke the function locally fine. I have also downloaded the zipped project from S3 and checked permissions etc.

The logs show that the requirements are installed correctly, but even then, just to make sure I tried commenting out everything in the function file except for a bare handler, no dependencies at all, and still fails.

Any ideas on why Lambda can fail to find my module?

2 Answers

I do not think it is supported to put the file in a subdirectory, so you probably have to make sure that the file function_one.py file is in the root of the zip file.

I think posting the question here served as rubber duck debugging! After hours trying to make it work, I have just found the problem: the Code URI entry in the template was pointing to a full path to a S3 bucket, like:

s3://aws-eu-west-2-575-foo-foo-pipe/622d85cc8

But I just need a ./, like this:

 Resources:
  getAllItemsFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: ./
      Handler: function_one.lambda_handler

I hope this helps someone else and they don't have to spend hours trying to debug this. Lambda is nice when it all works well, but man it is hard to debug.

Related