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
  • 2
    Better place to ask this would be stackoverflow site. – bravokeyl May 16 '17 at 14:52
  • 1
    How are you invoking Lambda function? Enable cloudwatch logs for the API and check, also check logs for lambda function, that will give you an idea of what's wrong. – bravokeyl May 16 '17 at 14:54
  • You say your coding effort does not give the desired result. Please do show the code. – mico May 16 '17 at 15:23
  • @Bex KeyError means key 'Id' is missing from the event parameter. What do you mean by "running it through the API Gateway" exactly? You make the mistake on that part. – mico May 17 '17 at 11:31
  • Show your effort on that part too. – mico May 17 '17 at 11:41
4

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

Your null should be

"null" 

https://stackoverflow.com/questions/43708017/aws-lambda-api-gateway-error-malformed-lambda-proxy-response

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.