3

I am not able to make my lambda ever return a value even though the code runs fine to right before the return statement. my return statement and the statement above it look like this:

print(score)
return {
        "StatusCode": 200,
        "headers":{'dummy':'dummy'},
        "body": str(score)
    }

Following is my serverless.yml:

service : test-deploy

plugins:
  - serverless-python-requirements
provider:
 name: aws
 runtime: python3.6
 region : ap-south-1
 deploymentBucket:
  name : smecornerdep-package
 iamRoleStatements:
  - Effect : Allow
    Action:
     - s3:GetObject
    Resource:
     - "arn:aws:s3:::smecornerdep/*"

custom:
 pythonRequirements:
  slim: True

functions:
 app-on-book-only:
  name: app-on-book-only
  description : deploy trained lightgbm on aws lambda using serverless
  handler : run_model_l.get_predictions
  events :
   - http : POST /engine

And I am hitting the end points with a POST from my command line like so:

curl -X POST -H "Content-Type: application/json" --data @sample_common_3.json https://76pmb6z1ya.execute-api.ap-south-1.amazonaws.com/dev/engine

In my aws lambda logs I can see the output of print(score) right above the return statement to be computed accurately. There are no errors in aws lambda logs. However, in my terminal I always get {"message": "Internal server error"} returned by the curl command.

I am a data scientist and fairly new to the concepts of dev ops. Please help me understand my mistake and also, suggest a solution.

Thank you for reading

2 Answers 2

2

I know whats wrong with your code. You are using the wrong syntax in the return dictionary StatusCode should be statusCode

return {
    "statusCode": 200,
    "headers":{'dummy':'dummy'},
    "body": str(score)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes .. You got it just right. It worked perfectly. Thank you
1

When you call a lambda with POST or GET ... need to have specific values in the return because the lambda use a proxy to be executed...

first... you have one bad header "StatusCode" -> "statusCode"

but you need this

return {
        "statusCode": 200,
        "body": json.dumps(score),
        "headers": {
            "Access-Control-Allow-Headers": "*",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "dummy": "dummy"
        },
        "isBase64Encoded": False
    }

If you don't have this... if you call your api gateway or lambda from the browser or another way (not postman or not command line) your function will not work :(

3 Comments

Thank you for the answer. "StatusCode" to "statusCode" did the trick
just remember the other parameters, if you call it from a browser or first go through an api gateway
Yes. Thank you for guidance!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.