Lambda Function, error reading "event" when sent from API Gateway

0

I have written a lambda function. And the trigger is an API which i created on AWS API Gateway. When I Test my event in the Lambda's Test environment, The Lambda function runs as expected. But, when i hit the Lambda function with same event through API Gateway, I get a key error about the event. Does anyone have any clue on how to resolve this?

2 Answers
0

You should check what the event structure is when you get it from API Gateway and write your code accordingly. I assume that your payload is embedded in a "body" attribute, that your code doesn't honor. Print the event object as the first line in the handler and then make sure to reference the keys as needed.

profile pictureAWS
EXPERT
Uri
answered 10 months ago
  • Hi Uri, Thanks for responding. For now, I have made an open API which doesnt require any keys, and using the Test event "{"key1": "value1"}" in Lambda Test environment as well as the API Gateway. The Lambda Test environment is working perfectly fine, And the API Gateway also Invokes the Lambda function if i do not use df=event['key1'] in the code. Whenever i use this line, it throws a key error. Basically, the event is getting considered NULL whenever I invoke the Lambda function through external server

  • It depends how you configured your API Gateway. The recommended way is to use Lambda proxy integration, and then you will get an event that contains the payload in the "body" attribute. If you did not set a proxy integration, you should create a mapping template to build the event that will be sent to Lambda.

0

Hi

Here you can see the Event Format sent from API gateway.

profile picture
EXPERT
answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions