简体   繁体   中英

Unable to import module 'lambda_function':

When I am trying to test my python code on AWS Lambda I am getting the following error.

Response
{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
  "errorType": "Runtime.ImportModuleError",
    "stackTrace": []
}

These are the basic Setup steps which I followed to push my code into AWS Lambda:-

  • First rename your main.py file as 'lambda_function.py'
  • Change the name of your 'main' function to 'def lambda_handler():'
  • ZIP the File
  • Go to AWS Lambda Console, Create Function.
  • Provide the FunctionName - 'Demo'
  • Choose Python 3.9 Runtime (or 3.7/3.8)
  • Let the architecture be 'x86_64'
  • Rest configurations be acceptable. Hit the 'Create Function' Button
  • There will be a tab/dropdown named 'Upload From'
  • Choose the.zip File
  • Upload the File
  • Click on the 'Test Button'
  • Click the configure test event.
  • Provide the event name as 'Test'
  • Clear the JSON Values and provide empty JSON- {}
  • Save it.
  • Test the Lambda Function

But after doing this I am getting this error

"errorMessage": "Unable to import module 'lambda_function': No module named 'lambda_function'",
  "errorType": "Runtime.ImportModuleError",

I have made sure to check my Handler Info .

My code - Here I am trying to invoke execute_AWS_TF() function using lambda_handler()

import connect_mongoDB

def lambda_handler():
    connect_mongoDB.execute_AMS_TF()

Working tree:

在此处输入图像描述

Directory Structure of my zip File:

在此处输入图像描述

Your lambda_function.py is in a subfolder called AWS_REPORT... . So you have the tell that to aws. The hendler info should be:

AWS_REPORT.../lambda_function.lambda_handler

Off course AWS_REPORT... must be replaced by the actual name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM