'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.
Solution 1:[1]
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']
Solution 2:[2]
Since, you have only mentioned
In my application, I will input data to invoke my lambda
I think you probably want to invoke lambda from you application.
For that,
- Add AWS credentials. Getting started with Python on AWS
- Import lambda from
boto.
import boto3
client = boto3.client('lambda')
- USE
invoke()to invoke a function with parameters:
response = client.invoke(
FunctionName='string',
InvocationType='Event'|'RequestResponse'|'DryRun',
LogType='None'|'Tail',
ClientContext='string',
Payload=b'bytes'|file,
Qualifier='string'
)
You can use official documentation to explore more Lambda boto Documentation
Also,
Lambda Function can be invoked in following ways:
- AWS services events (example: SNS triggered)
- API created through AWS API Gateway.
- Amazon CloudWatch cron jobs
- API calls leveraging AWS Lambda APIs.
Documentation: Ways to Invoke AWS Lambda Function
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | J. Parashar |
