figs_and_nuts
figs_and_nuts

Reputation: 5771

AWS lambda working without errors but i cannot return a value

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

Upvotes: 3

Views: 852

Answers (2)

Derek Menénedez
Derek Menénedez

Reputation: 2377

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 :(

Upvotes: 1

Pradeep Pathak
Pradeep Pathak

Reputation: 454

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)
}

Upvotes: 2

Related Questions