Amazon State Language Specification Example WITH CONDITION
{
"Comment": "An example of the Amazon States Language using a choice state.",
"StartAt": "FirstState",
"States": {
"FirstState": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012: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:us-east-1:123456789012:function:OnFirstMatch",
"Next": "NextState"
},
"SecondMatchState": {
"Type" : "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:OnSecondMatch",
"Next": "NextState"
},
"DefaultState": {
"Type": "Fail",
"Error": "DefaultStateError",
"Cause": "No Matches!"
},
"NextState": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:FUNCTION_NAME",
"End": true
}
}
}
Corresponding faasflow Example in openfaas WITH CONDITION
func Define(flow *faasflow.Workflow, context *faasflow.Context) (err error) {
dag := flow.Dag()
dag.Node("FirstState").Apply("FUNCTION_NAME")
conditionalDags := dag.ConditionalBranch("ChoiceState",
[]string{"1", "2", "default"}, // possible conditions
func(response []byte) []string {
result := &struct{
Foo int `json:"foo"`
}{}
json.Load(response, &result)
switch result.Foo {
case 1:
return []string{fmt.Sprintf("%s", result. Foo)}
case 2:
return []string{fmt.Sprintf("%s", result. Foo)}
}
return []string{ "default" }
},
)
conditionalDags["1"].Node("FirstMatchState").Apply("OnFirstMatch")
conditionalDags["2"].Node("SecondMatchState").Apply("OnSecondMatch")
conditionalDags["default"].Node("DefaultState").Modify(func(data []byte) ([]byte, error) {
return data, faasflow.AWSDefaultStateError
})
dag.Node("NextState").Apply("FUNCTION_NAME")
dag.Edge("FirstState", "ChoiceState")
dag.Edge("ChoiceState", "NextState")
}