Search code examples
amazon-web-servicesaws-step-functionsstate-machine-workflow

Get all the failed executions of a state machine in AWS Step function and execute dynamically. (In java)


I want to fetch all the failed executions and need to re-trigger them dynamically.

PS: In stepfunction definition I had proper retry mechanism, now I want to rerun the failed executions dynamically.

I need to implement it in java. Please help me with the approach.

Thanks in advance.


Solution

  • You can use the AWS Step Functions API to get a list of excutions:

    https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/SfnClient.html#listExecutions-

    Then you can get a list of ExecutionListItem by calling the executions() method that belongs to the ListExecutionsResponse object (returned by the listExecutions method) https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html

    Using this object - you can do two things:

    1 - check status - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionStatus.html

    2 - get state machine ARN value - https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/sfn/model/ExecutionListItem.html#stateMachineArn--

    Using the state machine ARN value, you can execute a state machine with the AWS Step Functions Java API V2:

    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.sfn.SfnClient;
    import software.amazon.awssdk.services.sfn.model.*;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.UUID;
    // snippet-end:[stepfunctions.java2.start_execute.import]
    
    public class StartExecution {
    
           public static void main(String[] args) {
                final String USAGE = "\n" +
                        "Usage:\n" +
                        "    StartExecution <stateMachineArn> <jsonFile>\n\n" +
                        "Where:\n" +
                        "    stateMachineArn - the ARN of the state machine.\n\n" +
                        "    jsonFile - A JSON file that contains the values to pass to the worflow.\n" ;
    
                if (args.length != 2) {
                    System.out.println(USAGE);
                    System.exit(1);
                }
    
                String stateMachineArn = args[0];
                String jsonFile = args[1];
                Region region = Region.US_EAST_1;
                SfnClient sfnClient = SfnClient.builder()
                        .region(region)
                        .build();
    
               String exeArn = startWorkflow(sfnClient,stateMachineArn, jsonFile);
               System.out.println("The execution ARN is" +exeArn);
               sfnClient.close();
            }
    
            // snippet-start:[stepfunctions.java2.start_execute.main]
            public static String startWorkflow(SfnClient sfnClient, String stateMachineArn, String jsonFile) {
    
                String json = getJSONString(jsonFile);
    
                // Specify the name of the execution by using a GUID value.
                UUID uuid = UUID.randomUUID();
                String uuidValue = uuid.toString();
                try {
    
                    StartExecutionRequest executionRequest = StartExecutionRequest.builder()
                            .input(json)
                            .stateMachineArn(stateMachineArn)
                            .name(uuidValue)
                            .build();
    
                    StartExecutionResponse response = sfnClient.startExecution(executionRequest);
                    return response.executionArn();
    
    
                } catch (SfnException e) {
                    System.err.println(e.awsErrorDetails().errorMessage());
                    System.exit(1);
                }
                return "";
            }
    
        private static String getJSONString(String path) {
    
            try {
                JSONParser parser = new JSONParser();
                JSONObject data = (JSONObject) parser.parse(new FileReader(path));//path to the JSON file.
                String json = data.toJSONString();
                return json;
            } catch (IOException |  org.json.simple.parser.ParseException e) {
                e.printStackTrace();
            }
            return "";
       }
        // snippet-end:[stepfunctions.java2.start_execute.main]
     }