"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
}
Author by
Admin
Updated on June 04, 2022Comments
-
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 about 7 yearsAppreciate 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 about 7 yearsIn 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 about 7 yearsAh 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 theSample event
list as tutorial suggests. -
Zohaib Ijaz about 7 yearsmy question is what do you want to achieve? tell me your use case?
-
Admin about 7 yearsDefinitely, wondering what's resolving the issue. Is it because I didn't provide
SITE = os.environ['site']
, yet trying to access it? -
Admin about 7 yearsAlso 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 about 7 yearsExactly. 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 about 7 yearsI missed
quotes
, changestatusCode
to"statusCode"
-
Zohaib Ijaz about 7 yearsAnd I think you know what is an upvote and selected answer :P XD
-
Admin about 7 yearsLet us continue this discussion in chat.