Skip to content

Instantly share code, notes, and snippets.

@s8sg
Created July 26, 2019 04:01
Show Gist options
  • Save s8sg/812ba71014d212446fad951a060832b5 to your computer and use it in GitHub Desktop.
Save s8sg/812ba71014d212446fad951a060832b5 to your computer and use it in GitHub Desktop.
Condition is Amazon States Language vs Condition is Faas-flow

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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment