[JAWS-UG CLI] StepFunctions #7 ステートマシンの実行 (RetryStateMachine)

前提条件
========


StepFunctionsへの権限
---------------------

StepFunctionsに対してフル権限があること。


AWS CLIのバージョン
-------------------

以下のバージョンで動作確認済

- AWS CLI 1.11.63

```bash:コマンド:
aws --version
```

結果(例):

      aws-cli/1.11.63 Python/2.7.10 Darwin/15.6.0 botocore/1.5.26

バージョンが古い場合は最新版に更新しましょう。

```bash:コマンド:
sudo -H pip install -U awscli
```


0. 準備
=======

まず変数の確認をします。

```bash:変数の確認:
cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        DIR_CONF:            (0.3) ${DIR_CONF}
        STEPF_STATEM_ARN:    (0.4) ${STEPF_STATEM_ARN}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE: (0.1) stepfunctionsas_full-prjZ-mbp13
      AWS_DEFAULT_REGION:  (0.2) ap-northeast-1
      DIR_CONF:            (0.3) conf-stepfunctions
      STEPF_STATEM_ARN:    (0.4) arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:stateMachine:RetryStateMachine

変数が入っていない、適切でない場合は、それぞれの手順番号について作業を
行います。


0.1. プロファイルの指定
-----------------------

プロファイルの一覧を確認します。

```bash:コマンド:
cat ~/.aws/credentials \
       | grep '\[' \
       | sed 's/\[//g' | sed 's/\]//g'
```

結果(例):

      iamFull-prjz-mbpr13
      stepfunctionsas_full-prjZ-mbp13

```bash:変数の設定:
export AWS_DEFAULT_PROFILE='stepfunctionsas_full-prjZ-mbp13'
```


0.2. リージョンの指定
---------------------

```bash:変数の設定:
export AWS_DEFAULT_REGION='ap-northeast-1'
```


0.3. 設定ファイル用ディレクトリの指定
-------------------------------------

```bash:変数の設定:
DIR_CONF='conf-stepfunctions'
```

```bash:コマンド:
mkdir -p ${DIR_CONF}
```


0.4. ステートマシンの指定
-------------------------

```bash:変数の設定:
STEPF_STATEM_NAME='RetryStateMachine'
```

```bash:コマンド:
STEPF_STATEM_ARN=$( \
        aws stepfunctions list-state-machines \
          --query "stateMachines[?name == \`${STEPF_STATEM_NAME}\`]".stateMachineArn \
          --output text \
) \
        && echo ${STEPF_STATEM_ARN}
```

結果(例):

      arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:stateMachine:RetryStateMachine


1. 事前作業
===========


1.1. 実行名の決定
-----------------

```bash:変数の設定:
STEPF_EXEC_NAME='retry-1'
```


1.2. 入力の指定
---------------

```bash:変数の設定:
FILE_INPUT="${DIR_CONF}/stepfunctions-exec-${STEPF_EXEC_NAME}.json" \
        && echo ${FILE_INPUT}
```

```bash:コマンド:
cat << EOF > ${FILE_INPUT}
{
        "Comment" : "AWS Step Functions"
}
EOF

cat ${FILE_INPUT}
```

JSONファイルを作成したら、フォーマットが壊れてないか必ず確認します。

```bash:コマンド:
jsonlint -q ${FILE_INPUT}
```

エラーが出力されなければOKです。


2. 実行
=======

```bash:変数の確認:
cat << ETX

        STEPF_STATEM_ARN: ${STEPF_STATEM_ARN}
        STEPF_EXEC_NAME:  ${STEPF_EXEC_NAME}
        FILE_INPUT:       ${FILE_INPUT}

ETX
```

```bash:コマンド:
aws stepfunctions start-execution \
        --state-machine-arn ${STEPF_STATEM_ARN} \
        --name ${STEPF_EXEC_NAME} \
        --input file://${FILE_INPUT}
```

結果(例):

      {
        "startDate": 1489733645.48,
        "executionArn": "arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:execution:RetryStateMachine:retry-1"
      }


3. 事後作業
===========

```bash:コマンド:
STEPF_EXEC_ARN=$( \
        aws stepfunctions list-executions \
          --state-machine-arn ${STEPF_STATEM_ARN} \
          --query "executions[?name == \`${STEPF_EXEC_NAME}\`].executionArn" \
          --output text \
) \
        && echo ${STEPF_EXEC_ARN}
```

結果(例):

      arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:execution:RetryStateMachine:retry-1

```bash:コマンド:
STEPF_EXEC_STATUS=$( \
        aws stepfunctions describe-execution \
          --execution-arn ${STEPF_EXEC_ARN} \
          --query 'status' \
          --output text \
) \
        && echo ${STEPF_EXEC_STATUS}
```


結果(例):

      SUCCEEDED


完了
====


アウトプットの確認
------------------

```bash:コマンド:
STEPF_EXEC_OUTPUT=$( \
        aws stepfunctions describe-execution \
          --execution-arn ${STEPF_EXEC_ARN} \
          --query 'output' \
          --output text \
) \
        && echo ${STEPF_EXEC_OUTPUT}
```


結果(例):

      "This is a fallback from a custom Lambda function exception"


実行ヒストリの確認
------------------

```bash:コマンド:
aws stepfunctions get-execution-history \
        --execution-arn ${STEPF_EXEC_ARN}
```

結果(例):

      {
        "events": [
          {
              "timestamp": 1489887775.672,
              "executionStartedEventDetails": {
                  "input": "{n  "Comment" : "AWS Step Functions"n}n",
                  "roleArn": "arn:aws:iam::XXXXXXXXXXXX:role/states-lambda-role"
              },
              "type": "ExecutionStarted",
              "id": 1,
              "previousEventId": 0
          },
          {
              "timestamp": 1489887775.732,
              "type": "TaskStateEntered",
              "id": 2,
              "stateEnteredEventDetails": {
                  "input": "{n  "Comment" : "AWS Step Functions"n}n",
                  "name": "CreateAccount"
              },
              "previousEventId": 0
          },
          {
              "timestamp": 1489887775.732,
              "lambdaFunctionScheduledEventDetails": {
                  "input": "{n  "Comment" : "AWS Step Functions"n}n",
                  "resource": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:FailFunction"
              },
              "type": "LambdaFunctionScheduled",
              "id": 3,
              "previousEventId": 2
          },
          {
              "timestamp": 1489887776.548,
              "type": "LambdaFunctionStarted",
              "id": 4,
              "previousEventId": 3
          },
          {
              "lambdaFunctionFailedEventDetails": {
                  "cause": "{"errorMessage":"Account is in use!","errorType":"AccountAlreadyExistsError","stackTrace":["exports.handler (/var/task/FailFunction.js:6:42)"]}",
                  "error": "AccountAlreadyExistsError"
              },
              "timestamp": 1489887776.96,
              "type": "LambdaFunctionFailed",
              "id": 5,
              "previousEventId": 4
          },
          {
              "timestamp": 1489887776.96,
              "stateExitedEventDetails": {
                  "output": "{"Error":"AccountAlreadyExistsError","Cause":"{\"errorMessage\":\"Account is in use!\",\"errorType\":\"AccountAlreadyExistsError\",\"stackTrace\":[\"exports.handler (/var/task/FailFunction.js:6:42)\"]}"}",
                  "name": "CreateAccount"
              },
              "type": "TaskStateExited",
              "id": 6,
              "previousEventId": 5
          },
          {
              "timestamp": 1489887776.988,
              "type": "PassStateEntered",
              "id": 7,
              "stateEnteredEventDetails": {
                  "input": "{"Error":"AccountAlreadyExistsError","Cause":"{\"errorMessage\":\"Account is in use!\",\"errorType\":\"AccountAlreadyExistsError\",\"stackTrace\":[\"exports.handler (/var/task/FailFunction.js:6:42)\"]}"}",
                  "name": "CustomErrorFallback"
              },
              "previousEventId": 6
          },
          {
              "timestamp": 1489887776.988,
              "stateExitedEventDetails": {
                  "output": ""This is a fallback from a custom Lambda function exception"",
                  "name": "CustomErrorFallback"
              },
              "type": "PassStateExited",
              "id": 8,
              "previousEventId": 7
          },
          {
              "executionSucceededEventDetails": {
                  "output": ""This is a fallback from a custom Lambda function exception""
              },
              "timestamp": 1489887776.988,
              "type": "ExecutionSucceeded",
              "id": 9,
              "previousEventId": 8
          }
        ]
      }