AWS. Lesson 5. Step Functions Tutorial

user1272047user1272047
4 min read

AWS Step Functions Tutorial


1. Creating a Simple Step Function

Example 1: Define a Basic Step Function

{
  "StartAt": "FirstStep",
  "States": {
    "FirstStep": {
      "Type": "Pass",
      "Next": "SecondStep"
    },
    "SecondStep": {
      "Type": "Succeed"
    }
  }
}

Explanation

  1. Defines the starting point of execution.

    • "StartAt": "FirstStep"
  2. FirstStep acts as a pass-through state.

    • "Type": "Pass", "Next": "SecondStep"
  3. SecondStep completes the execution successfully.

    • "Type": "Succeed"
  4. Creates a basic sequential workflow.

    • "Next": "SecondStep"

Example 2: Invoke a Lambda Function

{
  "StartAt": "CallLambda",
  "States": {
    "CallLambda": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda",
      "End": true
    }
  }
}

Explanation

  1. Defines a state that calls a Lambda function.

    • "Type": "Task", "Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda"
  2. Uses an AWS Lambda ARN for invocation.

    • "Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyLambda"
  3. Ends the step function after execution.

    • "End": true"
  4. Executes the function and stops the workflow.

    • "StartAt": "CallLambda"

Example 3: Choice State for Conditional Logic

{
  "StartAt": "CheckValue",
  "States": {
    "CheckValue": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.value",
          "NumericGreaterThan": 10,
          "Next": "Greater"
        }
      ],
      "Default": "LessThanOrEqual"
    },
    "Greater": { "Type": "Succeed" },
    "LessThanOrEqual": { "Type": "Succeed" }
  }
}

Explanation

  1. Checks if the input value is greater than 10.

    • "Variable": "$.value", "NumericGreaterThan": 10
  2. Routes execution to the ‘Greater’ state if true.

    • "Next": "Greater"
  3. Defaults to ‘LessThanOrEqual’ if condition fails.

    • "Default": "LessThanOrEqual"
  4. Enables branching workflows in Step Functions.

    • "Type": "Choice"

Example 4: Parallel Execution

{
  "StartAt": "ParallelExecution",
  "States": {
    "ParallelExecution": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "Task1",
          "States": { "Task1": { "Type": "Succeed" } }
        },
        {
          "StartAt": "Task2",
          "States": { "Task2": { "Type": "Succeed" } }
        }
      ],
      "End": true
    }
  }
}

Explanation

  1. Executes multiple branches simultaneously.

    • "Type": "Parallel"
  2. Defines two parallel branches, Task1 and Task2.

    • "Branches": [{ "StartAt": "Task1" }, { "StartAt": "Task2" }]
  3. Completes execution when both tasks finish.

    • "End": true"
  4. Improves performance by running independent tasks together.

    • "States": { "Task1": { "Type": "Succeed" } }

2. Passing Data Between Steps

Example 1: Input Data for Step Function

{
  "StartAt": "ProcessData",
  "States": {
    "ProcessData": {
      "Type": "Pass",
      "Result": { "message": "Hello, World!" },
      "Next": "OutputData"
    },
    "OutputData": {
      "Type": "Succeed"
    }
  }
}

Explanation

  1. Passes a static JSON object to the next step.

    • "Result": { "message": "Hello, World!" }
  2. Ensures data flows from ‘ProcessData’ to ‘OutputData’.

    • "Next": "OutputData"
  3. Useful for debugging and testing workflows.

    • "Type": "Pass"
  4. Completes the workflow when OutputData executes.

    • "Type": "Succeed"

Example 2: Use InputPath to Filter Incoming Data

{
  "StartAt": "FilterData",
  "States": {
    "FilterData": {
      "Type": "Pass",
      "InputPath": "$.details",
      "Next": "OutputFilteredData"
    },
    "OutputFilteredData": { "Type": "Succeed" }
  }
}

Explanation

  1. Extracts only the ‘details’ field from input.

    • "InputPath": "$.details"
  2. Removes unnecessary input fields.

    • "Type": "Pass"
  3. Sends the filtered data to the next state.

    • "Next": "OutputFilteredData"
  4. Ensures clean, optimized data processing.

    • "Type": "Succeed"

Example 3: Use ResultPath to Modify Output

{
  "StartAt": "TransformData",
  "States": {
    "TransformData": {
      "Type": "Pass",
      "Result": { "status": "processed" },
      "ResultPath": "$.processingInfo",
      "Next": "Done"
    },
    "Done": { "Type": "Succeed" }
  }
}

Explanation

  1. Adds new data without modifying original input.

    • "ResultPath": "$.processingInfo"
  2. Stores processed status separately.

    • "Result": { "status": "processed" }
  3. Combines existing and new data in workflow.

    • "Next": "Done"
  4. Useful for transforming input data.

    • "Type": "Pass"

Example 4: Using OutputPath

{
  "StartAt": "TrimData",
  "States": {
    "TrimData": {
      "Type": "Pass",
      "OutputPath": "$.trimmed",
      "Next": "Finish"
    },
    "Finish": { "Type": "Succeed" }
  }
}

Explanation

  1. Reduces the output to only the ‘trimmed’ field.

    • "OutputPath": "$.trimmed"
  2. Removes unnecessary data from the workflow output.

    • "Type": "Pass"
  3. Passes only the needed data to Finish state.

    • "Next": "Finish"
  4. Helps optimize Step Function execution costs.

    • "Type": "Succeed"

Next Steps

  • You can integrate Step Functions with Lambda, DynamoDB, SNS, and SQS.

  • More advanced scenarios, are: error handling, retries, or state machine triggers, etc.

0
Subscribe to my newsletter

Read articles from user1272047 directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

user1272047
user1272047