Search code examples
amazon-web-servicesaws-lambdaboto3

Getting AWS Lambda response when using boto3 invoke()


Im currently writing a python script that interacts with some AWS lambda functions. In one of the functions, my response contains a list which I need in my script.

Problem is that when I use the invoke() function, the response is a json which contains request information.

response = aws_lambdaClient.invoke(FunctionName = 'functionName', Payload = payload)

The function that im using has this as a return

return {'names': aList, 'status': 'Success!'}

If I print out the response, I get this:

{'ResponseMetadata': {'RequestId': 'xxxxxxxxx', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Thu, 07 Nov 2019 14:28:25 GMT', 'content-type': 'application/json', 'content-length': '51', 'connection': 'keep-alive', 'x-amzn-requestid': 'xxxxxxxxxx', 'x-amzn-remapped-content-length': '0', 'x-amz-executed-version': '$LATEST', 'x-amzn-trace-id': 'root=xxxxxxxxx;sampled=0'}, 'RetryAttempts': 0}, 'StatusCode': 200, 'ExecutedVersion': '$LATEST', 'Payload': <botocore.response.StreamingBody object at 0x0000023D15716048>}

And id like to get

{'names': aList, 'status': 'Success!'}

Any idea on how can I achieve this? Or should I find another way of getting the data (Maybe putting the list i need in an s3 bucket and then getting it from there).


Solution

  • Manuel,

    as mentioned, the return info is inside the Payload element in the returned json. Payload is a boto3 object type that you need to access it's contents through it's read() method.

    The code I used to get the python dictionary that I return from my lambda functions is this:

    payload = json.loads(response['Payload'].read())
    
    statusCode = payload.get('statusCode')
    message = payload.get('message')
    results = payload.get('results')