"errorMessage": "module initialization error"

12,798

You don't need any environment variables. Just keep it simple

from __future__ import print_function

import os
from datetime import datetime
from urllib2 import urlopen


def lambda_handler(event, context):
    url = 'https://www.google.com' # change it with your own
    print('Checking {} at {}...'.format(url, datetime.utcnow()))
    html = urlopen(url).read()
    # do some processing
    return html

Here is another simple example.

from __future__ import print_function


def lambda_handler(event, context):
    first = event.get('first', 0)
    second = event.get('second', 0)
    sum = first + second
    return sum

Here is a sample event which will be used to invoke this lambda. you can configure event from Lambda web interface. (or google it)

{
  "first": 10,
  "second": 23
}
Share:
12,798
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    Using Python, I followed and when it came to Test it, the following error popped up:

    {
      "errorMessage": "module initialization error"
    }
    

    What could I have done wrong?

  • Admin
    Admin about 7 years
    Appreciate your response! But do you mind explaining what you are doing for learning purposes? And where do I get the URL? And when would I need the environment variables?
  • Zohaib Ijaz
    Zohaib Ijaz about 7 years
    In this lambda function, we are retrieving an html page. but you lambda can do anything. e.g. you can add two numbers. let me change the code
  • Admin
    Admin about 7 years
    Ah got it! Thought that was the solution to the problem rather than an example. But in the examples provided, what really is the solution to the problem I encountered? Also, would like to use Scheduled Event in the Sample event list as tutorial suggests.
  • Zohaib Ijaz
    Zohaib Ijaz about 7 years
    my question is what do you want to achieve? tell me your use case?
  • Admin
    Admin about 7 years
    Definitely, wondering what's resolving the issue. Is it because I didn't provide SITE = os.environ['site'] , yet trying to access it?
  • Admin
    Admin about 7 years
    Also got a new error with the sum example provided: { "stackTrace": [ [ "/var/task/lambda_function.py", 9, "lambda_handler", "return {statusCode: 200, body: {'answer': sum}}" ] ], "errorType": "NameError", "errorMessage": "global name 'statusCode' is not defined" }
  • Zohaib Ijaz
    Zohaib Ijaz about 7 years
    Exactly. environment variables are set while creating lambda but in my opinion, passing site url through environment variable is not a good idea unless you have different environments like dev, staging, production etc. if you that value from environment variables, set that first
  • Zohaib Ijaz
    Zohaib Ijaz about 7 years
    I missed quotes , change statusCode to "statusCode"
  • Zohaib Ijaz
    Zohaib Ijaz about 7 years
    And I think you know what is an upvote and selected answer :P XD
  • Admin
    Admin about 7 years