7

An AWS lambda function has an "event" and a "context" as in parameters. The "event" is a json object.

I try to connect an API (manager through the AWS API Gateway) to my lambda function, sending the json of the event as the body of an http POST. This fails miserably, and I just have some indication that there might be an empty event sent to the lambda function.

How should I send the "event" through the API?

This is the code of my lambda function:

from __future__ import print_function

import boto3
import json
import time

print('Loading function')

def lambda_handler(event, context):
    print("Received event: ")
    print(type(event))
    print(""+json.dumps(event, indent=2))

    id = event['Id']
    dynamo = boto3.resource('dynamodb').Table('Table1')
    dynamo.put_item( 
        Item = {
        'Button' : int(id),
        'Time' : int(time.time()),
    })
    return {
        'statusCode' : '400',
        'body' : null,
        'headers' : { 'Content-Type': 'application/json', },
    }

Running a test on the lambda function gives the following log:

START RequestId: x Version: $LATEST
Received event: 
<type 'dict'>
{
  "Id": "1"
}
END RequestId: x

and the answer

{
   "body": null,
   "headers": {
     "Content-Type": "application/json"
  },
  "statusCode": "400"
}

but running it through the API Gateway test function gives

Tue May 16 15:54:27 UTC 2017 : Endpoint response body before transformations: 
  {"stackTrace": [["/var/task/lambda_function.py", 12, "lambda_handler", 
   "id = event['Id']"]], "errorType": "KeyError", "errorMessage": "'Id'"}
Tue May 16 15:54:27 UTC 2017 : Endpoint response headers: 
  {x-amzn-Remapped-Content-Length=0, x-amzn-RequestId=x, 
   Connection=keep-alive, Content-Length=153,
   X-Amz-Function-Error=Unhandled, Date=Tue, 16 May 2017 15:54:27 GMT, 
   X-Amzn-Trace-Id=root=x;sampled=0, Content-Type=application/json}
Tue May 16 15:54:27 UTC 2017 : Execution failed due to configuration 
   error: Malformed Lambda proxy response
Tue May 16 15:54:27 UTC 2017 : Method completed with status: 502
Bex
  • 683
  • 4
  • 16

1 Answers1

4

After some more investigation I found out that 502 error may happen if body is not in quotes.

Your null should be

"null" 

AWS lambda api gateway error "Malformed Lambda proxy response"

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
mico
  • 4,351
  • 1
  • 18
  • 27