how to get return response from AWS Lambda function

Python 2.7Aws Lambda

Python 2.7 Problem Overview


I have a simple lambda function that returns a dict response and another lambda function invokes that function and prints the response.

lambda function A

def handler(event,context):
    params = event['list']
    return {"params" : params + ["abc"]}

lambda function B invoking A

a=[1,2,3]
x = {"list" : a}
invoke_response = lambda_client.invoke(FunctionName="monitor-workspaces-status",
                                       InvocationType='Event',
                                       Payload=json.dumps(x))
print (invoke_response)

invoke_response

{u'Payload': <botocore.response.StreamingBody object at 0x7f47c58a1e90>, 'ResponseMetadata': {'HTTPStatusCode': 202, 'RequestId': '9a6a6820-0841-11e6-ba22-ad11a929daea'}, u'StatusCode': 202}

Why is the response status 202? Also, how can I get the response data from invoke_response? I could not find a clear documentation of how to do it.

Python 2.7 Solutions


Solution 1 - Python 2.7

A 202 response means Accepted. It is a successful response but is telling you that the action you have requested has been initiated but has not yet completed. The reason you are getting a 202 is because you invoked the Lambda function asynchronously. Your InvocationType parameter is set to Event. If you want to make a synchronous call, change this to RequestResponse.

Once you do that, you can get the returned data like this:

data = invoke_response['Payload'].read()

Solution 2 - Python 2.7

try: data = invoke_response['Payload'].read() read() because it is a StreamingBody object

<botocore.response.StreamingBody object at 0x110b91c50>

It is in the boto3 docs. You can find more details about this here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/resources.html#actions

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3089927View Question on Stackoverflow
Solution 1 - Python 2.7garnaatView Answer on Stackoverflow
Solution 2 - Python 2.7Akshansh ShrivastavaView Answer on Stackoverflow