Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the response results from an asynchronous call to AWS lambda in python

I have an AWS lambda function which I can call synchronously and get results back alright with below code

response = lambda_client.invoke(
        FunctionName=FUNCTION_NAME,
        InvocationType='RequestResponse',
        LogType='Tail',
        Payload=payload,
        Qualifier=$LATEST
    )

The response Payload is of type <botocore.response.StreamingBody object at 0x115fb3160> So I use below code to extract the payload which works fine.

response_body = response['Payload']
response_str = response_body.read().decode('utf-8')
response_dict = eval(response_str)

Now, I need to call my lambda asynchronously, so I change the invocation type with InvocationType='Event'

It gives me a response with payload of the same type as before, botocore.response.StreamingBody object but I am getting error with this line - response_dict = eval(response_str)

The error message says

    response_dict = eval(response_str)
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

What am I missing? If the response payload is same type as synchronous call, why is this parsing error? Any suggestion?

EDIT

For clarity, I understand that if the InvocationType='Event', then we only get the status of the invoke call, not the lambda function result. In my case though, I need both - launch the lambda async and get the result back when done. How do I do that? Is writing the result back to s3 and periodically checking that the only option?

like image 953
nad Avatar asked Sep 30 '19 22:09

nad


People also ask

How do I return AWS Lambda 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).

Is it possible to make a Lambda that executes asynchronously?

Lambda functions can be invoked either synchronously or asynchronously, depending upon the trigger. In synchronous invocations, the caller waits for the function to complete execution and the function can return a value.

How do you call async function in Lambda Python?

Async Python handlers are not supported by AWS Lambda. If you need to use async / await functionality in your AWS Lambda, you have to define an async function in your code (either in Lambda files or a Lambda Layer) and call asyncio. get_event_loop(). run_until_complete(your_async_handler()) inside your sync handler.


1 Answers

InvocationType='Event' means you aren't getting a response. An asynchronous Lambda invocation means you just want to invoke the function, not wait for the response. The response payload from the function is discarded by the service.

When you invoke a function asynchronously, Lambda sends the event to a queue. A separate process reads events from the queue and runs your function. When the event is added to the queue, Lambda returns a success response without additional information. (emphasis added)

https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html

Note that the queue mentioned here is a queue inside the Lambda service, not to be confused with Amazon Simple Queue Service (SQS).

like image 199
Michael - sqlbot Avatar answered Oct 04 '22 18:10

Michael - sqlbot