Step Functions annoncé comme un service de workflow. D'autres services de flux de travail incluent Simple Workflow (SWF), mais pour les nouvelles applications, il est recommandé de prendre en compte les fonctions d'étape. Si vous ne répondez pas à vos besoins, vous pouvez essayer SWF.
Il existe actuellement 6 types de plans disponibles sous forme d'exemples dans les fonctions Step. En regardant simplement ce code, il semble que vous puissiez comprendre les spécifications minimales requises.
Regardez le Blueprint préparé en premier et créez une machine d'état avec toutes sortes d'états définis à la fin.
Blueprint Hello World
{
"Comment": "A Hello World example of the Amazon States Language using a Pass state",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Pass",
"Result": "Hello World!",
"End": true
}
}
}
Wait State
{
"Comment": "An example of the Amazon States Language using wait states",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "wait_using_seconds"
},
"wait_using_seconds": {
"Type": "Wait",
"Seconds": 10,
"Next": "wait_using_timestamp"
},
"wait_using_timestamp": {
"Type": "Wait",
"Timestamp": "2015-09-04T01:59:00Z",
"Next": "wait_using_timestamp_path"
},
"wait_using_timestamp_path": {
"Type": "Wait",
"TimestampPath": "$.expirydate",
"Next": "wait_using_seconds_path"
},
"wait_using_seconds_path": {
"Type": "Wait",
"SecondsPath": "$.expiryseconds",
"Next": "FinalState"
},
"FinalState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
Retry Failure
{
"Comment": "A Retry example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Retry": [
{
"ErrorEquals": ["HandledError"],
"IntervalSeconds": 1,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.TaskFailed"],
"IntervalSeconds": 30,
"MaxAttempts": 2,
"BackoffRate": 2.0
},
{
"ErrorEquals": ["States.ALL"],
"IntervalSeconds": 5,
"MaxAttempts": 5,
"BackoffRate": 2.0
}
],
"End": true
}
}
}
Parallel
{
"Comment": "An example of the Amazon States Language using a parallel state to execute two branches at the same time.",
"StartAt": "Parallel",
"States": {
"Parallel": {
"Type": "Parallel",
"Next": "Final State",
"Branches": [
{
"StartAt": "Wait 20s",
"States": {
"Wait 20s": {
"Type": "Wait",
"Seconds": 20,
"End": true
}
}
},
{
"StartAt": "Pass",
"States": {
"Pass": {
"Type": "Pass",
"Next": "Wait 10s"
},
"Wait 10s": {
"Type": "Wait",
"Seconds": 10,
"End": true
}
}
}
]
},
"Final State": {
"Type": "Pass",
"End": true
}
}
}
Catch Failure
{
"Comment": "A Catch example of the Amazon States Language using an AWS Lambda Function",
"StartAt": "HelloWorld",
"States": {
"HelloWorld": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Catch": [
{
"ErrorEquals": ["HandledError"],
"Next": "CustomErrorFallback"
},
{
"ErrorEquals": ["States.TaskFailed"],
"Next": "ReservedTypeFallback"
},
{
"ErrorEquals": ["States.ALL"],
"Next": "CatchAllFallback"
}
],
"End": true
},
"CustomErrorFallback": {
"Type": "Pass",
"Result": "This is a fallback from a custom lambda function exception",
"End": true
},
"ReservedTypeFallback": {
"Type": "Pass",
"Result": "This is a fallback from a reserved error code",
"End": true
},
"CatchAllFallback": {
"Type": "Pass",
"Result": "This is a fallback from a reserved error code",
"End": true
}
}
}
Choice State
{
"Comment": "An example of the Amazon States Language using a choice state.",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"Next": "ChoiceState"
},
"ChoiceState": {
"Type" : "Choice",
"Choices": [
{
"Variable": "$.foo",
"NumericEquals": 1,
"Next": "FirstMatchState"
},
{
"Variable": "$.foo",
"NumericEquals": 2,
"Next": "SecondMatchState"
}
],
"Default": "DefaultState"
},
"FirstMatchState": {
"Type" : "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnFirstMatch",
"Next": "NextState"
},
"SecondMatchState": {
"Type" : "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:OnSecondMatch",
"Next": "NextState"
},
"DefaultState": {
"Type": "Fail",
"Cause": "No Matches!"
},
"NextState": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:FUNCTION_NAME",
"End": true
}
}
}
Les 7 types d'états suivants sont disponibles.
Créez la machine d'état suivante en utilisant les 7 types d'état.
JSON La machine à états ci-dessus est définie dans JSON comme ci-dessous.
{
"Comment": "Sample Step functions flow",
"StartAt": "Process1",
"States": {
"Process1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process1",
"Next": "ChoiceStep"
},
"ChoiceStep": {
"Type": "Choice",
"Choices": [{
"Variable": "$.processResult",
"StringEquals": "Process2a",
"Next": "SuccessProcess"
}, {
"Variable": "$.processResult",
"StringEquals": "Process2b",
"Next": "Process2b-1"
}]
},
"SuccessProcess": {
"Type": "Succeed"
},
"Process2b-1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Next": "Process2b-2"
},
"Process2b-2": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Catch": [{
"ErrorEquals": [
"HandledError"
],
"Next": "FailedProcess"
}],
"Next": "Process3"
},
"FailedProcess": {
"Type": "Fail"
},
"Process3": {
"Type": "Wait",
"Seconds": 30,
"Next": "Process4"
},
"Process4": {
"Type": "Parallel",
"Next": "Process5",
"Branches": [
{
"StartAt": "Process4a",
"States": {
"Process4a": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"End": true
}
}
},
{
"StartAt": "Process4b-1",
"States": {
"Process4b-1": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"Retry": [{
"ErrorEquals": [
"HandledError"
],
"IntervalSeconds": 3,
"MaxAttempts": 5,
"BackoffRate": 1.5
}],
"Next": "Process4b-2"
},
"Process4b-2": {
"Type": "Pass",
"Result": {
"data1": 12345,
"data2": -12345
},
"ResultPath": "$.test-data",
"Next": "Process4b-3"
},
"Process4b-3": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process2",
"End": true
}
}
}
]
},
"Process5": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXX:function:Process3",
"End": true
}
}
}
Préparez les trois fonctions Labda suivantes.
def lambda_handler(event, context):
event["step"] = 1
event["processResult"] = "Process2b"
return event
def lambda_handler(event, context):
event["step"] += 1
return event
def lambda_handler(event, context):
print event
return event
Sélectionnez [Fonctions d'étape] dans la console de gestion.
Sélectionnez Commencer.
Entrez ce qui suit et sélectionnez [Create State Machine]. Give a name to your state machine: SampleStateMachineWithVariableOfTypes
Entrez le JSON du précédent.
Lorsque vous appuyez sur le bouton de chargement de l'aperçu, l'état de la machine d'état est affiché.
IAM role for your state machine executions: StatesExecutionRole-us-east-1
{
"processResult": "Process2b"
}
Output
output: [{"processResult": "Process2b", "step": 4},{"test-data": {"data1": 12345, "data2": -12345}, "processResult": "Process2b", "step": 5}]
Tips -Vous pouvez vérifier l'exactitude de la syntaxe avec statelint. (J'ai l'impression qu'il y a des moments où c'est inexact.)
jq
.python sample.py
comme suit.def lambda_handler(event, context):
event["step"] = 1
event["processResult"] = "Process2b"
return event
if __name__ == "__main__":
lambda_handler("","")
――Je me fâche quand je définis une tâche supplémentaire
Recommended Posts