0

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.

4
  • 1
    Are you using lambda proxy integration? Commented Sep 26, 2020 at 14:48
  • Yes. I will update my answer to callout that I am using LAMBDA_PROXY integration Commented Sep 26, 2020 at 15:31
  • I tried and I can get "I have been stringified" successfully. Commented Sep 26, 2020 at 15:37
  • Dealing with the event and response format of Lambda is irritating sometimes, and difficult to read. I'm in the process of devlopping this library to make use of common AWS resources easier: github.com/shlublu/awsmate - the apigateway part is now quite stable. I hope it will help! Commented Apr 7, 2023 at 15:26

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

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.