Open nataliemona opened 3 years ago
you might want to try something like this.
import json
import traceback
def respond(err, res=None):
return {
"statusCode": "400" if err else "200",
"body": err["message"] if err else json.dumps(res),
"headers": {"Content-Type": "application/json"},
}
def lambda_handler(event, context):
try:
import sys
# from lib.inference import predict
except Exception as e:
error = str(e)
stacktrace = json.dumps(traceback.format_exc())
message = "Exception: " + error + " Stacktrace: " + stacktrace
err = {"message": message}
return respond(err)
return respond(None, "wow25520003")
i can't find the original link I got it from, but it will catch import errors.
Following Python 3.9 Alpine Linux instructions from: https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/
The only line I changed in the Dockerfile is:
COPY app/* ${FUNCTION_DIR}
toCOPY app.py ${FUNCTION_DIR}
app.py
entry.sh
Running:
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload": "hello world!"}'
Getting the error:{"errorMessage": "Unable to import module '': No module named ''", "errorType": "Runtime.ImportModuleError"}
This is hard to debug because the module is an empty string.
I am able to connect to the docker container with
docker exec -it <name> /bin/sh
and then run the handler directly withpython -c 'import app; print(app.handler({}, {}))'
. The handler runs with no errors if I run it directly.