AWS Step Functions Cheatsheet

0
7062
feature-image

What are AWS step functions?

Step functions is a service introduced by AWS (Amazon Web Services), it is a Serverless based service that allows the use of Lambda functions and other services in a combined way, it uses something called state machines to create a workflow with steps, each step can be customized to the need, for example, one step can be a task performed using Lambda function, another step could be interacting with AWS SNS or any other supported service.

These are useful in many situations, here are a few examples

  • When we want to break a big workflow where a lot of things are happening into smaller code portions or tasks.
  • When we want to handle error in a flexible way, which means performing custom operations on errors, calling a particular service, perform a previously done execution etc, aws step functions also provide a great way to retry and catch errors, there is a way to retry a particular logic if it fails to execute or even catch that error to execute a custom workflow.
  • When we want to perform a set of operations in a parcular order and with each step customized.
  • When we want to wait for certain human interaction to happen before executing the rest of the code.

To learn how to use AWS Step Functions with Serverless Framework with a practical example, check out how to Extract text from an image with AWS Textract using AWS Step functions on Nodejs

Most Imporant AWS Cli Commands

Create state machine

aws stepfunctions create-state-machine --name <string> --definition <string> --role-arn <string>

Explanation

name – This is the name of the state machine.

definition – This is the stringified JSON with all the steps and configuration mentioned for our state machine, it looks something like this –

{
  "Comment": "Some comment",
  "StartAt": "extractText",
  "States": {
    "extractText": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-2:175749735948:function:aws-step-functions-dev-extractText",
      "Next": "generatePdf"
    },
    "generatePdf": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-2:175749735948:function:aws-step-functions-dev-generatePdf",
      "End": true,
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 3,
          "BackoffRate": 2
        }
      ]
    }
  }
}

role-arn – This is the ARN of the role which you need to create, role policies will depend on what actions you want to perform in your state machine.

Know more

List all the state machine

aws stepfunctions list-state-machines

Explanation

This command returns an object with an array of all the state machines created, object looks something like this

{
    "stateMachines": [
        {
            "stateMachineArn": "arn:aws:states:us-east-2:175749735948:stateMachine:newStateMachine",
            "name": "newStateMachine",
            "type": "STANDARD",
            "creationDate": "2021-11-13T10:47:35.196000+05:30"
        },
        {
            "stateMachineArn": "arn:aws:states:us-east-2:175749735948:stateMachine:testmachine",
            "name": "testmachine",
            "type": "STANDARD",
            "creationDate": "2021-11-16T12:24:21.102000+05:30"
        }
    ]
}

Know more

Start State Machine Execution

aws stepfunctions start-execution --state-machine-arn <string> --input <string>

Explanation

This command is used to start any state machine.

state-machine-arn – This is the whole ARN of the state machine.

input – This is stringified JSON input that gets passed to the first step of the state machine.

Know more

Stop The Execution

aws stepfunctions stop-execution --execution-arn <string>

Explanation

This command stops the execution which got started.

execution-arn – This is not the ARN of our state machine, when we start executing any state machine, it returns us the execution ARN of that execution, we need to pass that value here.

Know more

Describe Any Execution

aws stepfunctions describe-execution --execution-arn <string>

Explanation

This command shows the status of the execution of the state machine, it returns an object like this.

{
    "executionArn": "arn:aws:states:us-east-2:175749735948:execution:testmachine:b7e6ecd0-ecaf-4297-aa88-0eea1e427f06",
    "stateMachineArn": "arn:aws:states:us-east-2:175749735948:stateMachine:testmachine",
    "name": "b7e6ecd0-ecaf-4297-aa88-0eea1e427f06",
    "status": "FAILED",
    "startDate": "2021-11-16T13:58:45.526000+05:30",
    "stopDate": "2021-11-16T13:58:45.560000+05:30",
    "input": "{}",
    "inputDetails": {
        "included": true
    }
}

Know more

Conclusion

These are some of the most used AWS Step functions CLI commands, there will be part 2 of this post, where I will explain some of the most used NodeJs API’s for step functions.

Part 2 is out here

LEAVE A REPLY

Please enter your comment!
Please enter your name here