I have created a simple python 3.7 lambda function:
import json
import boto3
s3 = boto3.client("s3")
def lambda_handler(event, context):
bucket = "nubi-data"
key = "core/user.json"
try:
data = s3.get_object(Bucket=bucket, Key=key)
json_data = data['Body'].read()
#return json_data
return {
'statusCode': 200,
"headers": {"Content-Type": "application/json"},
'body': json.loads(json_data)
}
except Exception as e:
print(e)
raise e
This function reads a json file from an s3 bucket. The json file looks like:
{ "id": 1, "name": "John", "pwd": "password" }
The function runs successfully when I test from within function editor screen in AWS console with the following output:
Response: { "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": { "id": 1, "name": "John", "pwd": "password" } }
Request ID: "f57de02f-44dd-4854-9df9-9f3a8c90031d"
Function Logs: START RequestId: f57de02f-44dd-4854-9df9-9f3a8c90031d Version: $LATEST END RequestId: f57de02f-44dd-4854-9df9-9f3a8c90031d REPORT RequestId: f57de02f-44dd-4854-9df9-9f3a8c90031d Duration: 260.70 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 84 MB
But when I test the function from the API Gateway, I get the error
Thu Mar 21 21:04:08 UTC 2019 : Endpoint response body before transformations: {"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": {"id": 1, "name": "John", "pwd": "password"}} Thu Mar 21 21:04:08 UTC 2019 : Execution failed due to configuration error: Malformed Lambda proxy response Thu Mar 21 21:04:08 UTC 2019 : Method completed with status: 502