1

I keep getting this error message from my Lambda Function:

Tue Aug 18 21:15:31 UTC 2020 : Execution failed due to configuration error: Malformed Lambda proxy response
Tue Aug 18 21:15:31 UTC 2020 : Method completed with status: 502

What is I am trying to do is have a user enter two numbers. The lambda function takes those numbers and runs them through a function which references a list. The output is a row from the list containing two numbers, a name, and a default message. My Lambda function is in Python 3.6 and I am using API gateway using a GET method. List_r5 is a python script with a small list:

import json
from math import cos, asin, sqrt
import List_r5

print('Loading function')

#Parse out query string parameters
def lambda_handler(event, context):
    usera_in = event['queryStringParameters']['usera_in']
    userb_in = event['queryStringParameters']['userb_in']

    print('UserA_In=' + usera_in)
    print('UserB_In=' + userb_in)

#Define the function
def dis(us1, ub1, us2, ub2):
    p = 0.017453292519943295
    a = 0.5 - cos((us2-us1)*p)/2 + cos(us1*p)*cos(us2*p) * (1-cos((ub2-ub1)*p)) / 2
    return 12742 * asin(sqrt(a))

def clo(database, input):
    return min(database, key=lambda p: dis(v['us'],v['ub'],p['us'],p['ub']))

    coord = {'us': usera_in, 'ub': userb_in}   
    output = clo(List_r5.data, coord)

    sta_output = output['NAME']
    us_output = output['us']
    ub_output = output['ub']

#Body of response object
    gResponse = {}
    gResponse['Sta'] = sta_output
    gResponse['UserA'] = us_output
    gResponse['UserB'] = ub_output
    gResponse['Message'] = 'All good on your end?'


#HTTP response object
    responseObject = {}
    responseObject['statusCode'] = 200
    responseObject['headers'] = {}
    responseObject['headers']['Content-Type'] = 'application/json'
    responseObject['body'] = json.dumps(gResponse)

#Return response object
    return responseObject
4
  • One obvious problem is that your clo function returns min(database...) immediately, meaning that the rest of the function, as well as the response object you actually want to return at the end of the function, becomes unreachable. Commented Aug 19, 2020 at 2:35
  • 1
    @DarthCadeus Thank you. I have edited the code and the clo function is used again for the variable output 5 lines down Commented Aug 19, 2020 at 2:38
  • @GK89 could you update your code snippet with the corrected clo function? Commented Aug 21, 2020 at 18:53
  • @MCI It is updated: it just returns the min value. I run the clo function a few lines down to generate output Commented Aug 21, 2020 at 18:55

2 Answers 2

1
+50

everything after def clo. . . to the end of the snippet is inside the clo function definition, so it will never be executed. Un-indent the code as so

def clo(database, input):
    return min(database, key=lambda p: dis(v['us'],v['ub'],p['us'],p['ub']))

coord = {'us': usera_in, 'ub': userb_in}   
output = clo(List_r5.data, coord)

sta_output = output['NAME']
us_output = output['us']
ub_output = output['ub']

#Body of response object
gResponse = {}
gResponse['Sta'] = sta_output
gResponse['UserA'] = us_output
gResponse['UserB'] = ub_output
gResponse['Message'] = 'All good on your end?'


#HTTP response object
responseObject = {}
responseObject['statusCode'] = 200
responseObject['headers'] = {}
responseObject['headers']['Content-Type'] = 'application/json'
responseObject['body'] = json.dumps(gResponse)

#Return response object
return responseObject
Sign up to request clarification or add additional context in comments.

Comments

0

It looks to me like an indentation problem. Try indenting the functions dis() and clo() along with its contents.

import json
from math import cos, asin, sqrt
import List_r5

print('Loading function')

#Parse out query string parameters
def lambda_handler(event, context):
    usera_in = event['queryStringParameters']['usera_in']
    userb_in = event['queryStringParameters']['userb_in']

    print('UserA_In=' + usera_in)
    print('UserB_In=' + userb_in)

    #Define the function
    def dis(us1, ub1, us2, ub2):
        p = 0.017453292519943295
        a = 0.5 - cos((us2-us1)*p)/2 + cos(us1*p)*cos(us2*p) * (1-cos((ub2-ub1)*p)) / 2
        return 12742 * asin(sqrt(a))

    def clo(database, input):
        return min(database, key=lambda p: dis(v['us'],v['ub'],p['us'],p['ub']))

    coord = {'us': usera_in, 'ub': userb_in}   
    output = clo(List_r5.data, coord)

    sta_output = output['NAME']
    us_output = output['us']
    ub_output = output['ub']

#Body of response object
    gResponse = {}
    gResponse['Sta'] = sta_output
    gResponse['UserA'] = us_output
    gResponse['UserB'] = ub_output
    gResponse['Message'] = 'All good on your end?'


#HTTP response object
    responseObject = {}
    responseObject['statusCode'] = 200
    responseObject['headers'] = {}
    responseObject['headers']['Content-Type'] = 'application/json'
    responseObject['body'] = json.dumps(gResponse)

#Return response object
    return responseObject

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.