Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I pass arguments to invoke AWS lambda function by using python?

In my application,I will input data to invoke my lambda function,how can I do it? Thanks a lot.

like image 733
Rick.Wang Avatar asked Jul 25 '26 23:07

Rick.Wang


1 Answers

The Payload parameter of the invoke function might be what you are looking for.

The format is "bytes or seekable file-like object" which means a bytes string, or an open filehandle or perhaps a BytesIO?

This works for me:

    import boto3
    import json
    import pprint

    params = {"day": "20220410"}
    client = boto3.client('lambda')
    response = client.invoke(
        FunctionName='my_lambda_function',
        InvocationType='RequestResponse',
        Payload=json.dumps(params).encode(),
    )
    pprint.pp(response['Payload'].read())

The lambda receives the params in the event dictionary:

def my_lambda_function(event, context):
    day = event['day']
like image 55
John Mee Avatar answered Jul 27 '26 13:07

John Mee