30 Aug

AWS Step Functions for networkers – Workflow definition

Step Function Workflow Graph

AWS has many great tools and products that may simplify your task. In day to day work, no matter if you are networks engineer, software administrator or have a different role in your organization you will perform small repetitive tasks to complete the bigger project. Writing a long, complex scripts or programs is a solution, but it is flexible? Step Functions is a good option in such cases.

Good programming rule is to create small functions to complete small chunk of work and then pass it on another one. So instead of writing one script that will log into 100 devices to fetch firmware version you create a small function that does it for one device and then you call it in a loop in other function passing the new IP as an argument. That is exactly what AWS Step Functions are meant for. Using this service you can create a flow of small tasks, each dependent on other if required, to complete bigger work. Let me show you the basics and how you can use it.

My example will be quite easy. I will create a state machine that will compare ASA version information stored in the object in S3 bucket with one read from the device via REST API. I will create Lambda functions to cover all three tasks. Reading from the bucket and the device will run in parallel then read values I will provide as variable to the third function.

Graph representation of this simple algorithm is below

Step Function Workflow Graph

Step Function Workflow Graph

This graph if from AWS Step Function console. You will always get defined workflow as visual representation – basing just on the workflow code might lead to many mistakes. How you describe the workflow? It is just a JSON structure in the format defined as Amazon States Language.

Create new state machine

AWS Step Functions are elements of the State Machine. Each State Machine consists of functions that here we call the tasks. Each task is a step that is triggered automatically by previous task or group of tasks and monitored for errors. That is the very simplified definition, but for this tutorial it is sufficient.

To create new State Machine go to AWS Step Functions and run the creator. Each machine has to have a unique name in the region you run it.

State Machine Name Definition

State Machine Name Definition

Next, you can choose one of the blueprints to get the template of the machine code. I created my machine using the Parallel blueprint. If you have the code prepared already, you can choose the Custom blueprint which is just empty.

Each machine definition is the JSON structure in the format described in Amazon States Language. The text area on the left is the editor where you put the code of the definition of your machine. On the right pane, you can generate the graph with the graphic representation of the code.

When you complete the machine definition, you can click the Create State Machine button. If you do not have proper IAM role created yet, the AWS will create one for you. Otherwise, you need to select IAM role from the list.

AWS Step Function IAM Role

AWS Step Function IAM Role

There is one significant disadvantage of AWS Step Functions – once you create State Machine you cannot edit it!

Machine definition

As you can see on the graph, my state machine is simple. Its code is not complicated as well.

{
  "Comment": "Example of simple AWS Step Function from Piotr Wojciechowski",
  "StartAt": "GetConfiguration",
  "States": {
    "GetConfiguration": {
      "Type": "Parallel",
      "Next": "CompareFirmwareVersion",
      "Branches": [
        {
          "StartAt": "GetVersionFromS3",
          "States": {
            "GetVersionFromS3": {
              "Type": "Wait",
              "Seconds": 20,
              "End": true
            }
          }
        },
        {
          "StartAt": "GetVersionFromASA",
          "States": {
            "GetVersionFromASA": {
              "Type": "Wait",
              "Seconds": 10,
              "End": true
            }
          }
        }
      ]
    },
    "CompareFirmwareVersion": {
      "Type": "Pass",
      "End": true
    }
  }
}


Let’s take a look at this code. In this very first example, I am not calling any Lambda functions yet. Each step is either just a pause or empty step. I will modify this when we create Lambda functions for this state machine – we need the ARN of the Lambda function to put it into the machine definition.

Each step is a nested JSON object described in States subtree. You refer to each of the states by its defined name. You define the first state in the StartAt key. Each state must have the type selected in key Type. We use three kinds h

ere – ParallelWait and Pass. In future, we will use Task state to call Lambda function. The Next key contains next step that we want to perform when the current one completes.

 

Running the Step Function

You will find all created Step Function on the dashboard. To run any of them, you need to select it – you will see then an execution history. To run State Machine click the New Execution button – you may provide the JSON structure with initial data if your State Machine require it. When you select the Start Execution button, you will see how State Machine executes each step, the input and output values of State Machine as well as of each step and exceptions if there are any problems. It is perfect for troubleshooting.

 

Leave a Reply

%d bloggers like this: