Fix AWS Python No module named lambda_function

Introduction

Fix AWS Python No module named lambda_function
Fix AWS Python No module named lambda_function

We will cover an easy Fix AWS Python No module named lambda_function.

Did you know that lambda functions need to be packed into a zip properly for this to work?

I will break this review in the following sections:

  • The problem of why you are getting AWS error from Python: No module named lambda_function
  • Show an example use-case of the problem
  • Demonstrate a solution with a bonus tip to resolve the problem

I have been using this fix successfully in many of my AWS Lambda accounts with success and it has been pretty effective.

I’m going to keep all the steps simple and straight to the point to avoid you have to spend endless hours debugging or trying to figure out why things aren’t working.

The Problem

The Problem
The Problem

The problem is that every lambda requires two things in order to work when you are doing your configuration:

  • Python file which is the entry point
  • Initial Python function to get called

By specifying any of these two incorrectly will lead with the error we went over earlier:

AWS error from Python: No module named lambda_function

Example Of Problem

Let’s see a quick example of the problem and why this may not work for you.

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  UnbiasedCoderFunction:
    Type: 'AWS::Serverless::Function'
    Properties:
      Handler: lambda_function.lambda_handler
      Runtime: python3.9
      CodeUri: .
      Description: ''
      MemorySize: 128
      Timeout: 3
      Role: >-
        arn:aws:iam::XXXX:role/service-role/UnbiasedCoderFunction-role-2fhb8yay

The example above was downloaded from the Lambda function export method in AWS web console that looks like this:

AWS error from Python: No module named lambda_function
AWS error from Python: No module named lambda_function

Once you click on the export function you will get the following dialog from here select Download AWS SAM File.

AWS error from Python: No module named lambda_function
AWS error from Python: No module named lambda_function

In the example above under the Properties section of the AWS Lambda Yaml file there’s the following line:

Handler: lambda_function.lambda_handler

This basically tells us two things:

  • The filename needs to be named lambda_function.py
  • The function that gets called needs to be named lambda_handler

From the above the problem lies if we try to upload a file to the AWS console that has any of the two properties different.

AWS error from Python: No module named lambda_function

Root Cause Of Problem

Root Cause
Root Cause

As described earlier the root cause is that either the filename or the entry point function is configured incorrectly when the Lambda code gets uploaded. If there’s a mismatch at any of those parts then execution will fail.

Solution

Solution
Solution

Now lets move on to the solution which in this case is pretty simple.

You have to basically match the python file and initial function name to the one specified in the Yaml file mentioned above. To make it work in our example we will be creating a zip with the single content of this file:

lambda.zip
|-> lambda_function.py

What this tells us is basically the file needs to be in the root of the ZIP package. Do not make a directory and include the directory in the root of the zip as this is one of the main causes of why it will not work!

Furthermore we need to do a further step here which is name our initial function as follows:

def lambda_handler(event, context):
    print ('hello Unbiased Coder')

This function name basically matches the lambda_handler name we specified.

To Test our theory we will perform the steps mentioned above to verify that everything is good.

Test To See If Lambda Function Executes

So we will go into our AWS console now and upload the file as follows:

Test To See If Lambda Function Executes
Test To See If Lambda Function Executes

Once you click on the upload using a ZIP file a dialog as shown below will popup.

Test To See If Lambda Function Executes
Test To See If Lambda Function Executes

In the dialog above you need to go ahead and basically upload the ZIP file as we discussed in the previous section, this will allow the AWS system to update the code of your AWS Lambda function as reflected in the screenshot below:

Update AWS Lambda Code
Update AWS Lambda Code

As shown above our code has been updated with the sample we provided earlier. This means we are now ready to test it out and see if it works. To do this you will need to create a Test event and then invoke it.

If you click on Text you will be prompted to select some event properties, since in our example we simply testing our theory to see if the solution works you can go ahead and put the event as empty shown below:

{
}

This basically tells the AWS Lambda function that we will not be passing any arguments to it. If your lambda requires some event parameters this is the place to put them in to proceed with the validation.

Now that we have the test event all setup we can go ahead and execute it to see if our fix was successful.

Test Event Name
tw

Response
null

Function Logs
START RequestId: d237671c-f1d4-4c26-b84f-cf78b143fed2 Version: $LATEST
hello from Unbiased Coder
END RequestId: d237671c-f1d4-4c26-b84f-cf78b143fed2
REPORT RequestId: d237671c-f1d4-4c26-b84f-cf78b143fed2	Duration: 1.51 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 36 MB	Init Duration: 103.28 ms

Request ID
d237671c-f1d4-4c26-b84f-cf78b143fed2

As shown above our lambda successfully executed which means our solution was valid. As you can see above the print out of  ‘hello from Unbiased Coder’ is shown and the Lambda session is then terminated.

Conclusion

We were able to successfully show how to demonstrate the Fix AWS Python No module named lambda_function.

If you found this useful and you think it may have helped you please drop me a cheer below I would appreciate it.

If you have any questions, comments please post them below or send me a note on my twitter. I check periodically and try to answer them in the priority they come in. Also if you have any corrections please do let me know and I’ll update the article with new updates or mistakes I did.

Do you use AWS Lambdas in your environment?

I like to use serverless functions as they are lightweight and serve their purpose in our micro-service driven world.

If you would like to learn more about AWS Lambda functions please check the links below:

You can find some references below:

Leave a Comment

Your email address will not be published. Required fields are marked *