Is it possible to set the "Start Execution" default JSON input via CDK for AWS Step Functions?

Viewed 723

I am automating the process of generating a Step Function for our Cloud Ops team to invoke. Is it possible to set the Execution default input parameters through the SDK? At present it defaults as below but I need to be able to set this as part of my stack deployment. Thanks

Input - optional Enter input values for this execution in JSON format

{
    "Comment": "Insert your JSON here"
}
2 Answers

No

Default input parameters are not supported by either Amazon States Language or the Step Functions CreateStateMachine API, though it would be a good feature request.

I'm guessing the default { "Comment": "Insert your JSON here" } is defined by the Step Functions Console, which you will not be able to control.

AWS Step Function exposes an API that lets you programmatically perform operations like invoking a workflow, listing workflows, etc. When you invoke a workflow using the API, you can specify the JSON you want to use. For example, here is the Java API example that invokes an existing workflow.

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]
 }

More examples here:

https://github.com/awsdocs/aws-doc-sdk-examples/tree/master/javav2/example_code/stepfunctions/src/main/java/com/example/stepfunctions

Related