{
  "StartAt": "GetProductsForAccount",
  "States": {
    "GetProductsForAccount": {
      "Type": "Task",
      "Resource": "${lambda_get_products_for_account_arn}",
      "TimeoutSeconds": 900,
      "Parameters": {
        "account_type.$": "$.account_type",
        "account_id.$": "$.account_id",
        "asset_types.$": "$.asset_types",
        "execution_name.$": "$$.Execution.Name",
        "export_id.$": "$.export_id"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "NotifyFailure",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "ProcessProducts"
    },
    "ProcessProducts": {
      "Type": "Map",
      "MaxConcurrency": 20,
      "ItemReader": {
        "Resource": "arn:aws:states:::s3:getObject",
        "ReaderConfig": {
          "InputType": "JSON"
        },
        "Parameters": {
          "Bucket": "${input_bucket_name}",
          "Key.$": "States.Format('{}/{}/products.json', $.export_id, $.execution_name)"
        }
      },
      "ItemProcessor": {
        "ProcessorConfig": {
          "Mode": "DISTRIBUTED",
          "ExecutionType": "STANDARD"
        },
        "StartAt": "GetAssetLocations",
        "States": {
          "GetAssetLocations": {
            "Type": "Task",
            "Resource": "${lambda_get_asset_locations_arn}",
            "TimeoutSeconds": 900,
            "Retry": [
              {
                "ErrorEquals": [
                  "States.Timeout",
                  "Lambda.ServiceException",
                  "Lambda.AWSLambdaException",
                  "Lambda.SdkClientException",
                  "Lambda.TooManyRequestsException"
                ],
                "IntervalSeconds": 5,
                "MaxAttempts": 5,
                "BackoffRate": 3
              }
            ],
            "Next": "DownloadAssets"
          },
          "DownloadAssets": {
            "Type": "Task",
            "Resource": "arn:aws:states:::ecs:runTask.sync",
            "TimeoutSeconds": 5184000,
            "Retry": [
              {
                "ErrorEquals": [
                  "States.Timeout"
                ],
                "IntervalSeconds": 60,
                "MaxAttempts": 3,
                "BackoffRate": 2
              }
            ],
            "Parameters": {
              "LaunchType": "FARGATE",
              "Cluster": "${fargate_asset_copy_cluster_arn}",
              "TaskDefinition": "${fargate_asset_copy_task_arn}",
              "NetworkConfiguration": {
                "AwsvpcConfiguration": {
                  "Subnets": [
                    "${subnet_id0}",
                    "${subnet_id1}"
                  ],
                  "AssignPublicIp": "DISABLED"
                }
              },
              "Overrides": {
                "ContainerOverrides": [
                  {
                    "Name": "${fargate_asset_copy_worker_name}",
                    "Environment": [
                      {
                        "Name": "execution_name",
                        "Value.$": "$.execution_name"
                      },
                      {
                        "Name": "export_id",
                        "Value.$": "$.export_id"
                      },
                      {
                        "Name": "part_index",
                        "Value.$": "States.Format('{}', $.part_index)"
                      }
                    ]
                  }
                ]
              }
            },
            "End": true
          }
        }
      },
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "NotifyFailure",
          "ResultPath": "$.errors"
        }
      ],
      "ResultWriter": {
        "Resource": "arn:aws:states:::s3:putObject",
        "Parameters": {
          "Bucket": "${input_bucket_name}",
          "Prefix.$": "States.Format('{}/{}', $.export_id, $.execution_name)"
        },
        "WriterConfig": {
          "Transformation": "FLATTEN",
          "OutputType": "JSON"
        }
      },
      "Next": "GenerateErrorReport"
    },
    "GenerateErrorReport": {
      "Type": "Task",
      "Resource": "${lambda_generate_error_report_arn}",
      "Parameters": {
        "bucket.$": "$.ResultWriterDetails.Bucket",
        "export_id.$": "States.ArrayGetItem(States.StringSplit($.ResultWriterDetails.Key, '/'), 0)",
        "execution_name.$": "States.ArrayGetItem(States.StringSplit($.ResultWriterDetails.Key, '/'), 1)",
        "map_run_id.$": "States.ArrayGetItem(States.StringSplit($.ResultWriterDetails.Key, '/'), 2)"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 2,
          "BackoffRate": 2.0
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "NotifyFailure"
        }
      ],
      "Next": "NotifySuccess"
    },
    "NotifySuccess": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "${sns_topic_arn}",
        "Subject": "[QA] Execution successful! 🎊",
        "Message.$": "States.Format('Execution {} is now complete and finished successfully. Happy bulk asset downloading!', $$.Execution.Name)"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 2,
          "BackoffRate": 2.0
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SuccessState"
        }
      ],
      "Next": "SuccessState"
    },
    "NotifyFailure": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sns:publish",
      "Parameters": {
        "TopicArn": "${sns_topic_arn}",
        "Subject": "[QA] Execution failed 😓",
        "Message.$": "States.Format('Execution {} failed to complete. Hrmph.', $$.Execution.Name)"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout"
          ],
          "IntervalSeconds": 1,
          "MaxAttempts": 2,
          "BackoffRate": 2.0
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "FailState"
        }
      ],
      "Next": "FailState"
    },
    "SuccessState": {
      "Type": "Succeed"
    },
    "FailState": {
      "Type": "Fail"
    }
  }
}
