Skip to content

Instantly share code, notes, and snippets.

@floer32
Created October 16, 2015 21:21
Show Gist options
  • Save floer32/e003bf90dc02f82740fc to your computer and use it in GitHub Desktop.
Save floer32/e003bf90dc02f82740fc to your computer and use it in GitHub Desktop.
A gotcha I ran into while playing with AWS Lambda.

I started out pasting Python code into the AWS Lambda web GUI, right in that textarea. Got it working, making a call to a Slack API (whee!). So I had a working test case and I could test a refactor.

With the same code, I wanted to take advantage of the cool zipfile/ virtualenv upload support ("Create a Deployment Package") so I could use third-party libraries. Keeping the code itself the same (same as what I'd pasted into the textarea), the test should work again, as long as I packaged it right.

I kept trying to upload a zip with the same code, I thought, to AWS Lambda ... but I kept getting an error:

"module initialization error"

Or ...

module initialization error: 'NoneType' object has no attribute 'close'

I was banging my head against the wall a bit, with this.

I figured out my problem and it was just that I got screwed up with overloaded or vague terms. I was thinking at the wrong level of modularity.

In AWS I could see my configuration said to use this Handler:

lambda_function.lambda_handler

So I had a file structure like this:

└── lambda_function
    ├── __init__.py
    └── lambda_handler.py

Both are wrong. lambda_function in the default configuration for AWS Lambda refers to a module, and lamdba_handler refers to a function within the module. Seems obvious in retrospect, but the names are vague, and if you're like me ... you're not used to putting module_name.function_name in an HTML form and having stuff work.

Anyway, this is the file structure that ACTUALLY matches the default configuration:

lambda_function.py

And that's it. lambda_function.py should have:

def lambda_handler(event, context):
    # ...

So you can get that working, and then if you want, you can rename things from there.

Happy hacking :)

Copy link

ghost commented Sep 2, 2016

haha, yeah i have everything setup correctly and i still am stuck with lambda... i used github/kappa for a bit but its not usable for my case :/

@floer32
Copy link
Author

floer32 commented Sep 16, 2016

@deeteecee-minion have you looked at https://www.zappa.io/ ? This is what I'll be using on my next Lambda project

@byrro
Copy link

byrro commented Nov 22, 2017

Just documenting my experience, as this shows when you search for lambda module initialization error on Google:

In my case, the error was trying to access an inexistent dictionary key using my_dict["invalid_key"]. Sometimes the lack of detailed error traces in Lambda makes me crazy, but in this case I managed to find it relatively quickly.

The reason it was working locally, but not on Lambda is that the dictionary key was a variable dependent on the environment. The error was only produced in my testing Lambda env.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment