0
votes

I am developing a new AWS Lambda function fronted by AWS API Gateway. Usually using python. I have done a ton of these before. Nevertheless, in this case I kept getting the error - "Execution failed due to configuration error: Malformed Lambda proxy response". HTTP 502. This was driving me nuts. In my case the mistake was trivial. Here is what I did wrong....

In my handlers return object (dict) I had "status" instead of "statusCode". Go figure, I used statusCode in all my other efforts. I wasted a few hours going round and round in circles here so I thought I would details that might help someone else fix to problem faster than me.

So... Sanity Test first: Here is a python specific sanity test

import json 
def lambda_handler(event, context):
    print(json.dumps(event)) 
    reply = {} 
    reply['body'] = json.dumps("I have been stringified") 
    reply["statusCode"] = 200 
    reply.update({"headers": {"Content-Type": "application/json"}})
    return reply

Next thing, if the statusCode is an integer or string it does not seem to matter. Both seem to work ok.

What about stringifying the Body? If it is a string already you do not need to bother. If it is another more structured type like a dict, you do.

Lastly, let me point out that you can always use the "Test" event feature in the AWS console for AWS Lambda. Just print out the event (like above) in the handler copy it, and create a test using it as the test input body.

Long story short - start with the sanity test above if you are banging your head against the wall.

I was using LAMBDA_PROXY integration in the above case.

1
Are you using lambda proxy integration?jellycsc
Yes. I will update my answer to callout that I am using LAMBDA_PROXY integrationDavid Kierans
I tried and I can get "I have been stringified" successfully.jellycsc

1 Answers

1
votes

I found a solution, that you actually faced.

Link to Documentation

It's happening because of your wrong format reply. Use the below code.

import json 
def lambda_handler(event, context):
    print(json.dumps(event)) 
    reply = {} 
    #No Need to do json.dumps() in every places
    reply['body'] = "I have been stringified" 
    reply["statusCode"] = 200 
    reply.update({"headers": {"Content-Type": "application/json"}})
    return reply