python - 如何使用用 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']
)
推荐阅读
- ios - 如何计算标签的最后一行宽度?
- flutter - 为什么升级到 Flutter 1.7 会报错?
- azure - 物联网中心和 TPM 配置
- wordpress - WooCommerce 如何按日期显示产品列表?现在它显示为按字母顺序
- java - 创建用于清理公司名称java的正则表达式
- ios - 如何在通话期间对来自麦克风的声音实时应用效果?
- python - 字典中键的多个值
- c# - How does PowerPoint distinguish which script out of latin/eastasian/complexscript is used for a Run?
- mysql - 在mysql中创建create语句时需要在一列中添加两个小时
- c# - 从 HUGE Xml 转换为 Json