Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling an AWS Lambda function asynchronously from python with async/await

I have an AWS lambda function that I need to call asynchronously (fire and forget) and get the result back when it is done in a non blocking way.

loop = asyncio.get_event_loop()

async def f(payload):
    lambda_client = boto3.client('lambda')
    response = lambda_client.invoke(
    FunctionName='FUNC_NAME',
    InvocationType='RequestResponse',
    LogType='Tail',
    Payload=payload,
    Qualifier='$LATEST'
    )
    response_body = response['Payload']
    response_str = response_body.read().decode('utf-8')
    response_dict = eval(response_str)
    return response_dict


async def g():

    payload = json.dumps({
      "test_bucket": "MY_BUCKET",
      "test_key": "my_test_key.csv",
      "testpred_bucket": "MY_BUCKET",
      "testpred_key": "my_test_key_new.csv",
      "problem": "APROBLEM"
    })
    # Pause here and come back to g() when f() is ready
    r = await f(payload)
    print(r)

This works but this does not really serve the purpose of fire and forget. I understand somehow I need to use asyncio.ensure_future but if I do asyncio.ensure_future(f(payload)), how do I capture the return value for the function f. I am new to python async and it is not clear.

Can anyone please suggest?

like image 459
nad Avatar asked Nov 21 '25 04:11

nad


1 Answers

What you need is to set InvocationType to 'Event'

import boto3

lambda_client = boto3.client('lambda')
lambda_payload = {"name":"name","age":"age"}
lambda_client.invoke(FunctionName='myfunctionname', 
                     InvocationType='Event',
                     Payload=lambda_payload)

for more info see: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke

like image 81
norman123123 Avatar answered Nov 23 '25 21:11

norman123123