{
  "Comment": "MeDaCa データ取込ECSタスク起動 ステートマシン",
  "StartAt": "params",
  "States": {
    "params": {
      "Type": "Pass",
      "Parameters": {
        "sns": {
          "TopicArn": "arn:aws:sns:#{REGION_AP_NORTHEAST_1}:#{AWS_ACCOUNT_ID}:nds-notice-#{ENV_NAME}"
        },
        "ecs": {
          "LaunchType": "FARGATE",
          "Cluster.$": "$.InputParams.ClusterName",
          "TaskDefinition.$": "$.InputParams.TaskName",
          "NetworkConfiguration": {
            "AwsvpcConfiguration": {
              "Subnets.$": "States.Array($.InputParams.SubNetIdApNorthEast1A, $.InputParams.SubNetIdApNorthEast1D)",
              "SecurityGroups.$": "States.Array($.InputParams.SecurityGroupIdECSAll, $.InputParams.SecurityGroupIdECSDataImport)",
              "AssignPublicIp": "DISABLED"
            }
          },
          "Overrides": {
            "ContainerOverrides": [
              {
                "Name.$": "$.InputParams.ContainerName",
                "Environment": [
                  {
                    "Name": "BUCKET_NAME",
                    "Value.$": "$.InputParams.BucketName"
                  },
                  {
                    "Name": "TARGET_KEY",
                    "Value.$": "$.InputParams.TargetKey"
                  },
                  {
                    "Name": "DATA_SOURCE_NAME",
                    "Value.$": "$.InputParams.DataSourceName"
                  },
                  {
                    "Name": "FILE_NAME",
                    "Value.$": "$.InputParams.FileName"
                  },
                  {
                    "Name": "MODE",
                    "Value.$": "$.InputParams.Mode"
                  }
                ]
              }
            ]
          }
        }
      },
      "Comment": "パラメータ設定",
      "ResultPath": "$.params",
      "Next": "CheckExclusive"
    },
    "CheckExclusive": {
      "Type": "Task",
      "Parameters": {
        "Bucket.$": "$.InputParams.BucketName",
        "Key.$": "States.Format('{}/target/.detected_fatal_error_ecs', $.InputParams.DataSourceName)"
      },
      "Resource": "arn:aws:states:::aws-sdk:s3:headObject",
      "ResultPath": "$.result",
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "data-import",
          "ResultPath": "$.result"
        }
      ],
      "Comment": "ECSタスク起動エラーファイル存在チェック",
      "Next": "PendingDataImportNotice"
    },
    "PendingDataImportNotice": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:sns:publish",
      "Parameters": {
        "TopicArn.$": "$.params.sns.TopicArn",
        "Subject": "データ登録ECSタスク異常通知",
        "Message.$": "States.Format('「{}/{}/target/」に登録処理中断ファイル(.detected_fatal_error_ecs)が見つかったため、データ登録の起動を中断しました。\n', $.InputParams.BucketName, $.InputParams.DataSourceName)"
      },
      "End": true,
      "Comment": "データ登録処理起動中断通知"
    },
    "data-import": {
      "Type": "Task",
      "Resource": "arn:aws:states:::ecs:runTask.sync",
      "Parameters": {
        "LaunchType.$": "$.params.ecs.LaunchType",
        "Cluster.$": "$.params.ecs.Cluster",
        "TaskDefinition.$": "$.params.ecs.TaskDefinition",
        "NetworkConfiguration.$": "$.params.ecs.NetworkConfiguration",
        "Overrides.$": "$.params.ecs.Overrides"
      },
      "ResultPath": "$.result",
      "Retry": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "BackoffRate": 2,
          "IntervalSeconds": 3,
          "MaxAttempts": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "CreateImportPendingFile",
          "ResultPath": "$.result"
        }
      ],
      "Next": "Success",
      "Comment": "データ登録処理"
    },
    "CreateImportPendingFile": {
      "Type": "Task",
      "Parameters": {
        "Body": "",
        "Bucket.$": "$.InputParams.BucketName",
        "Key.$": "States.Format('{}/target/.detected_fatal_error_ecs', $.InputParams.DataSourceName)"
      },
      "Resource": "arn:aws:states:::aws-sdk:s3:putObject",
      "Next": "FailedNotice",
      "Comment": "データ登録処理起動中断ファイル作成",
      "ResultPath": "$.result"
    },
    "FailedNotice": {
      "Type": "Task",
      "Resource": "arn:aws:states:::aws-sdk:sns:publish",
      "Parameters": {
        "TopicArn.$": "$.params.sns.TopicArn",
        "Subject": "データ登録ECSタスク異常通知",
        "Message.$": "States.Format('【{}】{}のデータ登録が失敗したため、登録処理中断ファイル(.detected_fatal_error_ecs)を作成しました。\n\n バケット名：{}\n取込ファイルパス：{}\n\n', $.InputParams.DataSourceName, $.InputParams.FileName, $.InputParams.BucketName, $.InputParams.TargetKey)"
      },
      "Next": "Fail",
      "Comment": "データ登録処理起動失敗通知"
    },
    "Success": {
      "Type": "Succeed",
      "Comment": "正常終了"
    },
    "Fail": {
      "Type": "Fail",
      "Comment": "異常終了",
      "Error": "StatusError",
      "Cause": "StepFunctions ErrorEnd"
    }
  }
}
