如何从AWS Lambda函数获取返回响应

问题描述:

我有一个简单的lambda函数,它返回一个dict响应,另一个lambda函数调用该函数并打印响应.

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

lambda函数A

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

lambda函数B调用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}

为什么响应状态为202?另外,如何从invoke_response获取响应数据?我找不到有关如何执行操作的清晰文档.

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.

202响应表示Accepted.这是一个成功的响应,但是告诉您您所请求的操作已启动但尚未完成.获得202的原因是因为您异步调用了Lambda函数.您的InvocationType参数设置为Event.如果要进行同步呼叫,请将其更改为RequestResponse.

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()