Python 3.x 简化的无服务器AWS step等待功能
我已经通过Serverless编写了AWS步骤函数。逻辑如下所示Python 3.x 简化的无服务器AWS step等待功能,python-3.x,serverless-framework,aws-step-functions,Python 3.x,Serverless Framework,Aws Step Functions,我已经通过Serverless编写了AWS步骤函数。逻辑如下所示 1. Execute Job 2. Get job status 3. Check if job succeeded 4. If not succeded wait for 60 seconds 现在,我已经编写了定义如下的step函数 ... stepFunctions: stateMachines: myStateMachine: definition: states:
1. Execute Job
2. Get job status
3. Check if job succeeded
4. If not succeded wait for 60 seconds
现在,我已经编写了定义如下的step函数
...
stepFunctions:
stateMachines:
myStateMachine:
definition:
states:
...
Execute Job:
...
Get Job Status:
Type: Task
Next: Is Job Succeeded?
# This job check if job is succeeded or not and sets a variable
# job_succeeded = true
Is Job Succeeded?
Type: Choice
Choices:
- Variable: "$.job_succeded"
BooleanEquals: true
Next: Next Lambda Function
Default: Wait for job
Wait for job:
Type: Wait
Seconds: 60
Next: Get Job Status
Next Lambda Function:
...
...
这非常有效强>
但问题是,我必须为异步执行的每个不同作业编写4个lambda,因此如果要完成4个这样的任务,那么将有4*4个步骤,即16个步骤
这使它看起来像是在步骤函数中有很多lambda,很难看到实际的流
这4项功能可概括为以下PSEDOO代码
job_succeded = false
while job is not complete:
job_succeded = get_job_status()
if not job_succeded:
sleep 60 seconds
潜在的解决方案可能是
stepFunctions:
stateMachines:
myStateMachine:
definition:
states:
...
Execute Job:
...
Get Job Status:
Type: RepeatWhile
Condition:
- Variable: "$.job_succeded"
BooleanEquals: true
Next: Next Lambda Function
WaitPeriod: 60 seconds
# This job check if job is succeeded or not and sets a variable
# job_succeeded = true
Next Lambda Function:
...
...
我知道这不是当前的情况,但这可以简化许多工作流程。
有没有其他方法可以简化此过程?欢迎使用step函数!不幸的是,他们写下来时非常臃肿。我不知道这是否有帮助,但对于像这样简单的事情,我将为我的所有任务使用一个lambda函数 您可以使用附加参数定义step函数,以告知函数要执行的内容
Task_1:
Type: Task
Resource:
Fn::GetAtt:
- MyLambdaFunction
- Arn
Parameters:
execute: task_1
Next: Task_2
Task_2:
Type: Task
Resource:
Fn::GetAtt:
- MyLambdaFunction
- Arn
Parameters:
execute: task_2
Next: Task_3
在lambda函数中,只需设置一些简单的if语句:
if (event.execute === 'task_1') {
// run task 1 code
}
else if (event.execute === 'task_2') {
// run task 2 code
}
我不太清楚你想在你的问题中做什么,但是你也可以从lambda输出变量,然后触发不同的步骤。您可以为此使用选择状态,例如
ChoiceState:
Type: Choice
Choices:
# response from lambda should be {executing: 'task_1', status: 'running'}
- Variable: "$.executing"
StringEquals: task_1
- Variable: "$.status"
StringEquals: running
Next: Wait
- And:
- Variable: "$.executing"
StringEquals: task_1
- Variable: "$.status"
StringEquals: done
Next: Task_2
- And:
- Variable: "$.executing"
StringEquals: task_2
- Variable: "$.status"
StringEquals: running
Next: Wait
- And:
- Variable: "$.executing"
StringEquals: task_2
- Variable: "$.status"
StringEquals: done
Next: Completed
Wait:
Type: Wait
Seconds: 10
Next: CheckExecution
Completed:
Type: Succeed
CheckExecution:
Type: Task
Resource:
Fn::GetAtt:
- MyLambdaFunction
- Arn
Next: ChoiceState
这不是一个完整的解决方案,因为在这里写下来太复杂了。但希望能给你一些额外的想法
此外,根据状态机的运行方式,此方法可在最大程度降低lambda冷启动时提供性能提升。感谢您的详细回答。我想你想说的是处理lambda中的控制流。但是,我想,step函数是用来处理工作流的,step函数应该控制工作流,而不是代码。lambda不控制工作流。除非阶跃函数要求,否则它不能做任何事情。Lambda只是照着说的做。Lambda从步骤函数获取输入,做一些事情,向步骤函数发送响应。步骤函数根据响应选择下一步。