Selective Checkpointing Example (Express Workflows) - Amazon Step Functions
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Selective Checkpointing Example (Express Workflows)

This sample project demonstrates how to combine Standard and Express Workflows by running a mock e-commerce workflow that does selective checkpointing. Deploying this sample project creates a Standard workflows state machine, a nested Express Workflows state machine, an Amazon Lambda function, an Amazon Simple Queue Service (Amazon SQS) queue, and an Amazon Simple Notification Service (Amazon SNS) topic.

For more information about Express Workflows, nested workflows, and Step Functions service integrations, see the following:

Create the State Machine and Provision Resources

  1. Open the Step Functions console and choose Create a state machine.

  2. Choose Run a sample project, and then choose Selective checkpointing example.

    The Standard Workflows state machine Code and Visual Workflow are displayed.

    
                    Selective checkpointing workflow.

    After the sample project is deployed, you can view the state machine Code and Visual Workflow of the nested Express Workflow.

  3. Choose Next.

    The Deploy resources page is displayed, listing the resources that will be created. For this sample project, the resources include:

    • A Step Functions state machine

    • An Amazon SQS queue

    • A Lambda function

  4. Choose Deploy Resources.

    Note

    It can take up to 10 minutes for these resources and related IAM permissions to be created. While the Deploy resources page is displayed, you can open the Stack ID link to see which resources are being provisioned.

After the resources of the sample project are deployed do the following.

Start a New Execution

  1. Open the Step Functions console.

  2. On the State machines page, choose the state machine that was created by the sample project and select Start execution.

  3. On the New execution page, enter an execution name (optional), and then choose Start Execution.

  4. (Optional) To identify your execution, you can specify a name for it in the Name box. By default, Step Functions generates a unique execution name automatically.

    Note

    Step Functions allows you to create state machine, execution, and activity names that contain non-ASCII characters. These non-ASCII names don't work with Amazon CloudWatch. To ensure that you can track CloudWatch metrics, choose a name that uses only ASCII characters.

  5. (Optional) Go to the newly created state machine on the Step Functions Dashboard, and then choose Start execution.

  6. When an execution is complete, you can open the Step Functions console.

  7. Go to your CloudWatch Logs log group and inspect the logs. The name of the log group will look like example-ExpressLogGroup-wJalrXUtnFEMI.

Example State Machine Code for the Parent (Standard Workflows)

The state machine in this sample project integrates with Amazon SQS , Amazon SNS, and Step Functions Express Workflows.

Browse through this example state machine to see how Step Functions processes input from Amazon SQS and Amazon SNS, and then uses a nested Express Workflows state machine to update backend systems.

For more information about how Amazon Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.

{ "Comment": "An example of combining standard and express workflows to run a mock e-commerce workflow that does selective checkpointing.", "StartAt": "Approve Order Request", "States": { "Approve Order Request": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sqs:sendMessage.waitForTaskToken", "Parameters": { "QueueUrl": "<SQS_QUEUE_URL>", "MessageBody": { "MessageTitle": "Order Request received. Pausing workflow to wait for manual approval. ", "TaskToken.$": "$$.Task.Token" } }, "Next": "Notify Order Success", "Catch": [ { "ErrorEquals": [ "States.ALL" ], "Next": "Notify Order Failure" } ] }, "Notify Order Success": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sns:publish", "Parameters": { "Message": "Order has been approved. Resuming workflow.", "TopicArn": "<SNS_ARN>" }, "Next": "Process Payment" }, "Notify Order Failure": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sns:publish", "Parameters": { "Message": "Order not approved. Order failed.", "TopicArn": "<SNS_ARN>" }, "End": true }, "Process Payment": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sqs:sendMessage.waitForTaskToken", "Parameters": { "QueueUrl": "<SQS_QUEUE_URL>", "MessageBody": { "MessageTitle": "Payment sent to third-party for processing. Pausing workflow to wait for response.", "TaskToken.$": "$$.Task.Token" } }, "Next": "Notify Payment Success", "Catch": [ { "ErrorEquals": [ "States.ALL" ], "Next": "Notify Payment Failure" } ] }, "Notify Payment Success": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sns:publish", "Parameters": { "Message": "Payment processing succeeded. Resuming workflow.", "TopicArn": "<SNS_ARN>" }, "Next": "Workflow to Update Backend Systems" }, "Notify Payment Failure": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sns:publish", "Parameters": { "Message": "Payment processing failed.", "TopicArn": "<SNS_ARN>" }, "End": true }, "Workflow to Update Backend Systems": { "Comment": "Starting an execution of an Express workflow to handle backend updates. Express workflows are fast and cost-effective for steps where checkpointing isn't required.", "Type": "Task", "Resource": "arn:<PARTITION>:states:::states:startExecution.sync", "Parameters": { "StateMachineArn": "<UPDATE_DATABASE_EXPRESS_STATE_MACHINE_ARN>", "Input": { "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id" } }, "Next": "Ship the Package" }, "Ship the Package": { "Type": "Task", "Resource": "arn:<PARTITION>:states:::sns:publish", "Parameters": { "Message": "Order and payment received, database is updated and the package is ready to ship.", "TopicArn": "<SNS_ARN>" }, "End": true } } }

Example IAM Role for the Parent State Machine

These example Amazon Identity and Access Management (IAM) policies generated by the sample project include the least privilege necessary to execute the state machine and related resources. We recommend that you include only those permissions that are necessary in your IAM policies.

Amazon SNS policy:

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "sns:Publish" ], "Resource": "arn:aws:sns:us-east-1:123456789012:Checkpoint-SNSTopic-wJalrXUtnFEMI", "Effect": "Allow" } ] }

Amazon SQS policy:

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "sqs:SendMessage" ], "Resource": "arn:aws:sqs:us-east-1:123456789012:Checkpoint-SQSQueue-je7MtGbClwBF", "Effect": "Allow" } ] }

States execution policy:

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "states:StartExecution", "states:DescribeExecution", "states:StopExecution" ], "Resource": "*", "Effect": "Allow" }, { "Action": [ "events:PutTargets", "events:PutRule", "events:DescribeRule" ], "Resource": "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventsForStepFunctionsExecutionRule", "Effect": "Allow" } ] }

Example State Machine Code for the Nested State Machine (Express Workflows)

The state machine in this sample project updates backend information when called by the parent state machine.

Browse through this example state machine to see how Step Functions updates the different components of the mock e-commerce backend systems.

For more information about how Amazon Step Functions can control other Amazon services, see Using Amazon Step Functions with other services.


                Selective checkpointing workflow.
{ "StartAt": "Update Order History", "States": { "Update Order History": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "FunctionName": "Checkpoint-UpdateDatabaseLambdaFunction-wJalrXUtnFEMI", "Payload": { "Message": "Update order history." } }, "Next": "Update Data Warehouse" }, "Update Data Warehouse": { "Type" : "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "FunctionName": "Checkpoint-UpdateDatabaseLambdaFunction-wJalrXUtnFEMI", "Payload": { "Message": "Update data warehouse." } }, "Next": "Update Customer Profile" }, "Update Customer Profile": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "FunctionName": "Checkpoint-UpdateDatabaseLambdaFunction-wJalrXUtnFEMI", "Payload": { "Message": "Update customer profile." } }, "Next": "Update Inventory" }, "Update Inventory": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "Parameters": { "FunctionName": "Checkpoint-UpdateDatabaseLambdaFunction-wJalrXUtnFEMI", "Payload": { "Message": "Update inventory." } }, "End": true } } }

Example IAM Role for Child State Machine

This example Amazon Identity and Access Management (IAM) policy generated by the sample project includes the least privilege necessary to execute the state machine and related resources. We recommend that you include only those permissions that are necessary in your IAM policies.

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "lambda:InvokeFunction" ], "Resource": [ "arn:aws-cn:lambda:us-east-1:123456789012:function:Example-UpdateDatabaseLambdaFunction-wJalrXUtnFEMI" ], "Effect": "Allow" } ] }

The following policy ensures that there are sufficient permissions for CloudWatch Logs.

{ "Version": "2012-10-17", "Statement": [ { "Action": [ "logs:CreateLogDelivery", "logs:GetLogDelivery", "logs:UpdateLogDelivery", "logs:DeleteLogDelivery", "logs:ListLogDeliveries", "logs:PutResourcePolicy", "logs:DescribeResourcePolicies", "logs:DescribeLogGroups" ], "Resource": [ "*" ], "Effect": "Allow" } ] }

For information about how to configure IAM when using Step Functions with other Amazon services, see IAM Policies for integrated services.