Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get return response from AWS Lambda function

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.

like image 310
user3089927 Avatar asked Apr 22 '16 04:04

user3089927


People also ask

Does Lambda function have return statement?

The lambda functions do not need a return statement, they always return a single expression.

Can AWS Lambda return a value?

Returning a valueIf you use the RequestResponse invocation type, such as Synchronous invocation, AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON).

How does AWS Lambda return the result of a call?

If you use the RequestResponse invocation type, such as Synchronous invocation, AWS Lambda returns the result of the Python function call to the client invoking the Lambda function (in the HTTP response to the invocation request, serialized into JSON).

How do I run Lambda on AWS S3?

Start the lambda function. Download data from a dummy API to local file system. Copy the downloaded files to AWS S3. Stop the lambda function. The lambda function will be scheduled to run every 5 minutes. Let’s assume you work for a company that wants to pull some data from an API that you have access to and assess the quality of that data.

How do I make a lambda function return a string?

The Lambda Placeholder. Create a simple Lambda function that returns an HTML string. Log into to the AWS Console and navigate to the Lambda service. Create a new Lambda function and select the hello-world template. Name the function lambda-html. Input the code block below to return some basic html.

How does AWS Firecracker work with Lambda?

Lambda functions run inside an AWS Firecracker container. When you return from your Lambda function, AWS immediately freezes the container and its global state. When the Lambda is next invoked, it will thaw the container to service the new invocation.


1 Answers

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() 
like image 137
garnaat Avatar answered Sep 21 '22 03:09

garnaat