如何解决如何使用 Python 编写的单个 AWS Lambda 函数一次调用/触发不同的多个 AWS Step Functions?
请注意以下 AWS Lambda 函数(在 python 代码中)以调用单个 AWS Step Function:
import boto3
def lambda_handler(event,context):
print("stating to invoke the AWS Step function.")
response = {"code":400,"message":""}
try :
stepFunction = boto3.client('stepfunctions')
response = stepFunction.start_execution(
stateMachineARN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
)
except Exception as e:
response["message"] = f"Failed to invoke the step function,{e}"
finally:
print(response)
return response
现在,我想使用上述单个 AWS Lambda 函数一次调用不同的多个 AWS Step 函数(串行和并行)。谁能帮我修改上面的代码?
解决方法
这是基础知识,但要小心,不要在另一个 lambda 函数中意外调用此函数,否则会陷入永久循环
def trigger_my_other_lambda(event,context):
invoked_function_name = os.environ['INVOKED_FUNCTION_NAME']
# this lambda function invokes another lambda function
# invoke other lambda as many times as in tasks_list or other range
for myarn in arnslist:
'''pass data in form of dictionary; this shows up and must be parsed by the event argument of receiving lambda'''
data = {"stateMachineARN" : myarn}
# print(data)
response = client.invoke(
FunctionName=invoked_function_name,# InvocationType='RequestResponse',# synchornous - waits for response
InvocationType='Event',# asynch - much faster - doesn't wait for response
Payload=json.dumps(data)
)
然后您现有的 lambda 将使用这样的事件:
stepFunction = boto3.client('stepfunctions')
response = stepFunction.start_execution(
stateMachineARN = event['stateMachineARN']
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。