APi Gateway, Lambda & Terraform: Get requests in json and respond in json

0

I provisioned a lambda function and api gateway resources in terraform, you know, the usual api gateway in front of lambda backend stuff. When I tried to return a response, i put a dictionary object in the body like this:

evt ={"pin": "some value"}

return {"statusCode": 200, "body": evt}

but it didn't work, i got an error saying malformed lambda response. Apparently the value of body needs to be a string. So it went through when I did this:

evt ={"pin": "some value"}

return {"statusCode": 200, "body": json.dumps(evt)}

So I need your help, how do i configure the API Gateway in terraform to make sure I can respond with a json/dictionary. I don't know why the event comes in as json (i checked) but I can't send back json.

These are the api gateway resources i have terraform: aws_api_gateway_rest_api aws_api_gateway_base_path_mapping aws_api_gateway_deployment aws_api_gateway_stage aws_api_gateway_rest_api_policy aws_api_gateway_resource aws_lambda_permission aws_api_gateway_method aws_api_gateway_integration aws_api_gateway_method_response aws_api_gateway_integration_response

PS: I don't want to use a swagger file.

1 Antwort
-1

Hello! This is not a problem in your resources, but in your Lambda's code. You must change the code to dump any python dictionaries that will be contained in your response into jsons.

I supose that "evt" is the Lambda event, right ? The event in python is always a dictionary when you receive it, you must work it in code before returning it.

If you have any doubts please let me know.

beantwortet vor 8 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen