ninsignares
ninsignares

Reputation: 29

How to invoke Lambda function using boto3 and passing parameters

This time I'm looking for passing parameters to lambda invoke using boto3.client instead of make a request to API gateway.

Assume I developed a lambda function that can be invoked through api get. ie: https://linktolambda/<parameter> and now I am trying to access same lambda function from another one using boto3 client.

I have read documentation from:

I have read stackoverflow questions, reddit questions, medium posts, etc. but I haven't found what I'm looking for.

Thanks in advance.

Upvotes: 2

Views: 11646

Answers (3)

smac2020
smac2020

Reputation: 10704

Look at the Official AWS Code Example Github. For Python Code examples look here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/python

You can find how to invoke a Lambda function and pass parameters (exactly what you are looking for) in this code example:

https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/python/example_code/lambda/lambda_basics.py

Look for the method invoke_function.

Upvotes: 0

Irene
Irene

Reputation: 94

There are actually two methods in BOTO3 that can be used to invoke a Lambda function. The first is: [invoke(**kwargs)][1]

and the second is: [invoke_async(**kwargs)][2]

See:

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

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/lambda.html#Lambda.Client.invoke_async

The first method makes a synchronous call and the second method makes an asynchronous call. Once your Lambda function has been deployed, you can use either method to invoke it.

Upvotes: 0

hoangdv
hoangdv

Reputation: 16127

If you don't want to update your lambda function, just simulate APIGateway event object by boto3 client:

If your api looks like https://linktolambda/{id} (ex: https://linktolambda/123456)

You will invoke with this code:

        payload = { "pathParameters": { "id":"123456" } } 
        result = client.invoke(FunctionName=conf.lambda_function_name,
                    InvocationType='RequestResponse',                                      
                    Payload=json.dumps(payload))

Or your API look like https://linktolambda?id=123456

        payload = { "queryStringParameters": { "id":"123456" } } 
        result = client.invoke(FunctionName=conf.lambda_function_name,
                    InvocationType='RequestResponse',                                      
                    Payload=json.dumps(payload))

Upvotes: 3

Related Questions