{
    "Comment": "A state machine that checks for pending images, then starts, analyzes and stops Rekognition model if needed.",
    "StartAt": "Check Pending Images",
    "States": {
        "Check Pending Images": {
            "Type": "Task",
            "Resource": "${CheckPendingImagesFunctionArn}",
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 15,
                    "MaxAttempts": 2,
                    "BackoffRate": 1.5
                }
            ],
            "Next": "Has Pending Images"
        },
        "Has Pending Images": {
            "Type": "Choice",
            "Choices": [
                {
                    "Variable": "$.should_start_model",
                    "BooleanEquals": true,
                    "Next": "Start Model"
                }
            ],
            "Default": "No Pending Images"
        },
        "No Pending Images": {
            "Type": "Succeed"
        },
        "Start Model": {
            "Type": "Task",
            "Resource": "${StartModelFunctionArn}",
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 15,
                    "MaxAttempts": 1,
                    "BackoffRate": 1.5
                }
            ],
            "Next": "Wait For Model Startup"
        },
        "Wait For Model Startup": {
            "Type": "Wait",
            "Seconds": 60,
            "Next": "Check Model Availability"
        },
        "Check Model Availability": {
            "Type": "Task",
            "Resource": "${CheckModelAvailabilityFunctionArn}",
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 15,
                    "MaxAttempts": 2,
                    "BackoffRate": 1.5
                }
            ],
            "Next": "Is Model Available"
        },
        "Is Model Available": {
            "Type": "Choice",
            "Choices": [
                {
                    "Variable": "$.model_available",
                    "BooleanEquals": true,
                    "Next": "Analyze Image"
                }
            ],
            "Default": "Wait For Model Startup"
        },
        "Analyze Image": {
            "Type": "Task",
            "Resource": "${AnalyzeImageFunctionArn}",
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 15,
                    "MaxAttempts": 2,
                    "BackoffRate": 1
                }
            ],
            "Next": "Stop Model"
        },
        "Stop Model": {
            "Type": "Task",
            "Resource": "${StopModelFunctionArn}",            
            "Retry": [
                {
                    "ErrorEquals": [
                        "States.TaskFailed"
                    ],
                    "IntervalSeconds": 20,
                    "MaxAttempts": 5,
                    "BackoffRate": 10
                }
            ],
            "End": true
        }
    }
}