简体   繁体   English

如何使用 python 传递参数来调用 AWS lambda 函数?

[英]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?在我的应用程序中,我将输入数据来调用我的 lambda 函数,我该怎么做? Thanks a lot.非常感谢。

The Payload parameter of the invoke function might be what you are looking for. 调用函数Payload参数可能就是您要查找的内容。

The format is "bytes or seekable file-like object" which means a bytes string, or an open filehandle or perhaps a BytesIO?格式是“字节或可搜索的类文件对象” ,表示字节字符串、打开的文件句柄或可能是 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: lambda 接收事件字典中的参数:

def my_lambda_function(event, context):
    day = event['day']

Since, you have only mentioned因为,你只提到

In my application, I will input data to invoke my lambda在我的应用程序中,我将输入数据来调用我的 lambda

I think you probably want to invoke lambda from you application.我认为您可能想从您的应用程序中调用 lambda。

For that,为了那个原因,

import boto3    
    client = boto3.client('lambda')
  • USE invoke() to invoke a function with parameters:使用invoke()调用带参数的函数:
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您可以使用官方文档来探索更多Lambda boto 文档

Also,还,

Lambda Function can be invoked in following ways:可以通过以下方式调用 Lambda 函数:

  • AWS services events (example: SNS triggered) AWS 服务事件(例如:SNS 触发)
  • API created through AWS API Gateway.通过 AWS API Gateway 创建的 API。
  • Amazon CloudWatch cron jobs Amazon CloudWatch 定时任务
  • API calls leveraging AWS Lambda APIs.利用 AWS Lambda API 的 API 调用。

Documentation: Ways to Invoke AWS Lambda Function文档: 调用 AWS Lambda 函数的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python中的lambda函数中传递参数 - How to pass arguments in lambda function in python 如何使用 Python 将参数传递给 AWS Lambda 函数 - How to pass parameters to an AWS Lambda Function using Python 如何使用 python 编写的单个 AWS Lambda Function 一次调用/触发不同的多个 AWS Step Functions? - How to invoke / trigger different multiple AWS Step Functions at a time , using a single AWS Lambda Function written in python? 如何使用 aws lambda function 在 Z23EEEB4347BDD755DDBZ3 中调用 bash 脚本? - how to invoke bash script with aws lambda function in python? 如何使用 GET 请求将参数传递给 AWS Lambda 函数? - How do I pass arguments to AWS Lambda functions using GET requests? 如何使用上下文参数调用 AWS lambda function - How to invoke AWS lambda function with context argument 如何使用Lambda将元组传递给python函数 - How to pass a tuple to a python function using lambda 我应该如何将我的s3凭据传递给AWS上的Python lambda函数? - How should I pass my s3 credentials to Python lambda function on AWS? 如何使用python调用带参数的第二个lambda函数 - How to invoke second lambda function with parameter using python 如何使用 Python 检索 AWS Lambda 公共 IP 地址? - How could I retrieve AWS Lambda public IP address by using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM