GK89
GK89

Reputation: 664

Lambda Function working, but cannot work with API Gateway

I have a working Lambda function when I test it using a test event:

{
  "num1_in": 51.5,
  "num2_in": -0.097
}


import json
import Function_and_Data_List

#Parse out query string parameters
def lambda_handler(event, context):
    num1_in = event['num1_in']
    num2_in = event['num2_in']
    coord = {'num1': num1_in, 'num2': num2_in}
    output = func1(Function_and_Data_List.listdata, coord)
    return {
        "Output": output
    }

However, when I use API gateway to create a REST API I keep getting errors. My method for the REST API are: 1.) Build REST API 2.) Actions -> Create Resource 3.) Actions -> Create Method -> GET 4.) Integration type is Lambda Function, Use Lambda Proxy Integration 5.) Deploy

What am I missing for getting this API to work?

Upvotes: 1

Views: 699

Answers (1)

Marcin
Marcin

Reputation: 238827

If you use lambda proxy integration, your playload will be in the body. You seem also having incorrect return format.

Therefore, I would recommend trying out the following version of your code:

import json
import Function_and_Data_List

#Parse out query string parameters
def lambda_handler(event, context):

    print(event)

    body = json.loads(event['body'])

    num1_in = body['num1_in']
    num2_in = body['num2_in']

    coord = {'num1': num1_in, 'num2': num2_in}
    output = func1(Function_and_Data_List.listdata, coord)

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

In the above I also added print(event) so that in the CloudWatch Logs you can inspect the event object which should help debug the issue.

Upvotes: 1

Related Questions