Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

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 the response status 202? Also, how to get the response data from invoke_response? I could not find clear documentation of how to do it.

1 Answer

0 votes
by (44.4k points)

202 response means that it is Accepted.

Even though it is a response for success, it tells you that the action which you requested has been started but yet to be completed.

To stop this asynchronous call where your InvocationType parameter is set to Event, you have to change it to RequestResponse. And after you do that, you can get the returned data like this:

data = invoke_response['Payload'].read()  //This will return bytes

If you want it in string or JSON, try this:

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

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...