2

So I'm trying to set up a function in AWS Lambda to run some python code I imported from a zip.

file name

I've edited the handler to run the file then the function I want to run hander

function I want to run

I've tried having the file in the directory created when I imported the zip folder, after which I I moved it to the main function directory. Neither worked

Not too sure what is wrong here, the full error returned when I run test is:

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

Edit: really new to Lambda so please excuse any silly mistakes

scotmanDavid
  • 139
  • 1
  • 9
  • Your Lambda function handler should be `def runJobs(event, context)`, not `def runJobs()`. It will be called with 2 positional parameters. This is definitely a problem with your code that must be fixed but probably not the initial problem with your code, which is the import failure. Make sure you've saved all changes and deployed all current code. What other code is in main.py? What else is in your deployment ZIP file? – jarmod Aug 12 '21 at 19:45
  • There are other functions that are called by runJobs.py, the other file is a json file with a key for me to edit google sheets. What do I changes do I need to put for the event and context parameters in the function? – scotmanDavid Aug 12 '21 at 19:53
  • Simply writing the correct function declaration, as I indicated above, is needed. So, I'd suggest that you write a very basic version of main.py (without all your other code) that includes `def runJobs(event, context)` and does something trivial, like `print(42)`. Deploy, then test. Does it work? – jarmod Aug 12 '21 at 19:55
  • So I've edited my function `def runJobs(event, context)` and added a print("that worked") and I still get the same respone `Response { "errorMessage": "Unable to import module 'main': No module named 'main'", "errorType": "Runtime.ImportModuleError", "stackTrace": [] }` – scotmanDavid Aug 12 '21 at 19:59
  • You've mentioned both main.py and runJobs.py. Do they both exist? What is the full list of files that Lambda shows? Are you sure that you've saved and deployed the code? – jarmod Aug 12 '21 at 20:09
  • runJobs is the name of the function I want to run in the file main.py – scotmanDavid Aug 12 '21 at 20:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/235953/discussion-between-jarmod-and-scotmandavid). – jarmod Aug 12 '21 at 20:11

3 Answers3

2

The problem is that, while you appear to have a module named main, it has not been deployed to the Lambda service yet. When you click Test, Lambda runs the deployed code. Perhaps your module was renamed to main some time after your initial deployment?

Local changes to code need to be saved and then deployed. The deploy step is important because until you deploy the code, the Lambda service will continue to run the previous code.

This has actually been a common problem historically in the Lambda console, but enhancements have been made to make it more obvious that a deployment is needed. For example the console now indicates "Changes not deployed" after you make a change, until you hit the Deploy button.

jarmod
  • 71,565
  • 16
  • 115
  • 122
  • 2
    Thank you very much for your amazing help and answer. For any future reader, the deploy is important as it 'updates' the code changes made (just a very simple explanation for newbies). My whole issue was that historically I was told to never 'deploy' code before testing it and that was the barrier between getting the issue solved. – scotmanDavid Aug 13 '21 at 05:43
2

I found this question while facing the problem myself. Issue was that the zip put "main.py" in a subfolder.

Hope this is helpful for any others!

Tom Petit
  • 33
  • 5
0

I had a similar issue when creating a portfolio chatbot for my website. When compressing the files into a zip file, a subfolder was automatically created, which caused problems with Lambda requiring the main.py file to be in the main folder.

To solve this issue, I used Powershell to compress the files. After navigating to the folder where my main project was located (in this case, the Portfolio folder was inside the Termproject folder), I executed the following command:

Compress-Archive -Path .\portfolioChatbot* -DestinationPath portfolioChatbot.zip -Force

This command compresses all the files within the portfolioChatbot folder and saves them as a zip file called portfolioChatbot.zip. The -Force flag is used to overwrite any existing files with the same name.

Hope this helps!