AWS Lambda-调用另一个lambda函数的方法 [英] AWS Lambda - Invoke a method of another lambda function
问题描述
我正在尝试从另一个lambda调用一个不同于默认处理程序方法的方法.但是却没有怎么做.从文档中尚不清楚.这是我的代码
I am trying to invoke a method, different than the default handler method, of one lambda from another lambda. But not getting how to do it. It is not clear from the documentation. Here is my code
Lambda函数1: my_function1
Lambda function 1: my_function1
import json
import boto3
def lambda_handler(event, context):
lambda_inv = boto3.client("lambda", region_name="us-east-1")
payload = {"message":"Hi From my_function1"}
lambda_inv.invoke(FunctionName='arn:aws:lambda:us-east-1:1236547899871:function:my_function2',
InvocationType='Event', Payload=json.dumps(payload))
Lambda函数2: my_function2
Lambda function 2: my_function2
import json
def lambda_handler(event, context):
# TODO implement
print("lambda_handler")
def say_hello(event, context):
print("From say_hello function")
print(str(event))
print("say_hello end")
我想从lambda my_function1 中调用lambda my_function2 的 say_hello 方法.我怎么做?默认情况下,它尝试调用默认的lambda_handler方法
I want to invoke say_hello method of lambda my_function2 from lambda my_function1. How do I do that? By default it tries to invoke the default lambda_handler method
推荐答案
Lambda函数始终通过处理函数输入.这就像主要方法".每个Lambda函数都是其自己的应用程序,具有自己的资源,因此,在它们之间进行协调时,您将始终通过main(处理程序)进入该函数,然后可以在其中进行任何其他操作.
Lambda Functions are always entered through the handler function. It is like the 'main method'. Each Lambda Function is its own application, with its own resources, so when you coordinate between them, you will always enter the function through main (the handler), where you can then go anywhere else.
请记住,每个Lambda在被调用之间都会丢失其所有资源(内存和CPU),因此它将始终在每次调用时从处理程序中重新启动.
要使用say_hello函数,您需要在处理程序中使用某些if语句,例如@jimmone描述的.像这样:
To get to your say_hello function, you'll need to use some if statements in the handler like @jimmone described. Something like this:
def lambda_handler(event, context):
lambda_inv = boto3.client("lambda", region_name="us-east-1")
payload = {
"message":"Hi From my_function1",
"request": "say_hello"
}
lambda_inv.invoke(FunctionName='arn:aws:lambda:us-east- 1:1236547899871:function:my_function2',
InvocationType='Event', Payload=json.dumps(payload))
def lambda_handler(event, context):
# TODO implement
print("lambda_handler")
if event['request'] == 'say_hello':
return say_hello(event, context)
def say_hello(event, context):
print("From say_hello function")
print(str(event))
print("say_hello end")
如果您只想更改处理程序的名称,可以通过在AWS Lambda中编辑Handler选项来完成.在这种情况下,my_function2.say_hello
If you are just wanting to change the name of the handler, that can be done by editing the Handler option in AWS Lambda. In this case my_function2.say_hello
这篇关于AWS Lambda-调用另一个lambda函数的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!