Search code examples
pythonamazon-web-servicesprintingreturnaws-lambda

AWS Lambda function appending characters in return


I have 2 Lambda Functions [A and B]. Function A does a computation and returns 2 strings. Note that I've also tried returning one string. When the function is called alone, the return is the correct expected string.

If I call function A inside function B, the return is the correct string but with characters added to each side.

Function A1 (two strings returned):

def handler(event, context):

    strings = {
           "first_string": "This is the first string",
           "second_string": "This is the second string"
    }

    return strings

Function A2 (one string returned):

def handler(event, context):

    string = "This is a string"

    return string

Calling A1 in another Lambda Function:

return_strings = functionA1(event, context)
print(return_strings[0])
print(return_strings[1])

>>> 341 #expected This is the first string
>>> 8 #expected This is the second string

Calling A2 in another Lambda function:

return functionA2(event, context)

>>> b'\"This is a string\"' #expected This is a string

Any idea what might be encoded in the returns - is it related to calling from another Lambda function? Invoking A1/A2 on their own gives expected returns.

Thanks!


Solution

  • Found the problem! Decoding needed before reading the JSON response:

    load = json.loads(response['Payload'].read().decode("utf-8"))