Sukruti
Sukruti

Reputation: 101

AWS Step Function indefinite execution

After an year, can the step function be restarted or is there someway to run the step function indefinitely which polls Amazon DynamoDB every 5 minutes?

Upvotes: 0

Views: 392

Answers (1)

smac2020
smac2020

Reputation: 10704

There is a way to invoke AWS Step Functions every 5 mins using the AWS SDK and Lambda functions. You can use the Step FUnctions Client to invoke the workflow created with Step Functions.

For example, this can be done with this Java code (this can be achieved using other AWS SDK supported programming languages).

package com.example.stepfunctions;

// snippet-start:[stepfunctions.java2.start_execute.import]
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sfn.SfnClient;
import software.amazon.awssdk.services.sfn.model.SfnException;
import software.amazon.awssdk.services.sfn.model.StartExecutionRequest;
import software.amazon.awssdk.services.sfn.model.StartExecutionResponse;
import java.io.FileReader;
import java.io.IOException;
import java.util.UUID;
// snippet-end:[stepfunctions.java2.start_execute.import]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class StartExecution {

       public static void main(String[] args) {

           final String usage = "\n" +
                    "Usage:\n" +
                    "    <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 workflow.\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)
                    .credentialsProvider(ProfileCredentialsProvider.create())
                    .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]
 }

Using the Java Lambda run-time API, create a Lambda function that uses the above Java code. Once you deploy the Lambda function, you can configure it to run according to a CRON Expression. Once setup, the Lambda function will execute and it will kick off your workflow based on the Cron expression.

Upvotes: 0

Related Questions