Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the current state of a running Step Functions in AWS

I'm giving the AWS' Step Functions a try and I'm interested in them for implementing long-running procedures. One functionality I would like to provide to my users is the possibility of showing execution's progress. Using describeExecution I can verify if some execution is still running or done. But progress is a logical measure and Step Functions itself has no way to tell me how much of the process is left.

For that, I need to provide the logic myself. I can measure the progress in the tasks of the state machine knowing the total number of steps needed to take and counting the number of steps already taken. I can store this information in the state of the machine which is passed among steps while the machine is running. But how can I extract this state using API? Of course, I can store this information is an external storage like DynamoDb but that's not very elegant!

like image 271
Mehran Avatar asked Jun 19 '18 13:06

Mehran


People also ask

How can you visually see the steps of your state machine within AWS Step Functions?

To view the results of your execution, in the visual workflow pane, choose World, and then, under Step details, choose Output. The output is World has been updated!

What is state in step function?

States are elements in your state machine. A state is referred to by its name, which can be any string, but which must be unique within the scope of the entire state machine. States can perform a variety of functions in your state machine: Do some work in your state machine (a Task state)

What is state machine in AWS Step Functions?

In Step Functions, a workflow is called a state machine, which is a series of event-driven steps. Each step in a workflow is called a state. A Task state represents a unit of work that another AWS service, such as AWS Lambda, performs. A Task state can call any AWS service or API.

What is state transition in Step Functions?

With AWS Step Functions, you pay only for the transition from one step of your application workflow to the next, called a state transition. Billing is metered by state transition, regardless of how long each state persists (up to one year).


1 Answers

The solution I have found my self (so far this is the only), is using getExecutionHistory API. This API returned a list of events that are generated for the Step Functions and it can include input or output (or neither) based on whether the event is for a starting a lambda function or is it for the time a lambda function has exited. You can call the API like this:

var params = {
  executionArn: 'STRING_VALUE', /* required */
  maxResults: 10,
  reverseOrder: true
};
stepfunctions.getExecutionHistory(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

By reversing the order of the list of events, we can get the latest ones first. Then we can look for the latest output in the list. The first one you'll find will be the latest version of the output which is the current state of the Step Functions.

like image 173
Mehran Avatar answered Oct 15 '22 17:10

Mehran