1

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?

2
  • What are the errors you get? Do you have necessary permission for API Gateway to invoke lambda function? Commented Aug 24, 2020 at 22:10
  • @PubuduJayawardana I am an Admin - the errors are { "message": "Internal server error" }Endpoint response body before transformations: {"errorMessage": "'num1_in'", "errorType": "KeyError", "stackTrace": [["/var/task/lambda_function.py", 18, "lambda_handler", "num1_in = event['num1_in']"]]} Mon Aug 24 22:16:25 UTC 2020 : Lambda execution failed with status 200 due to customer function error: 'num1_in'. Lambda request id: XXXXXX Mon Aug 24 22:16:25 UTC 2020 : Method completed with status: 502 Commented Aug 24, 2020 at 22:16

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.