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

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


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}
        STEPF_STATEM_ARN:    (0.3) ${STEPF_STATEM_ARN}

ETX
```

結果(例):

      AWS_DEFAULT_PROFILE: (0.1) stepfunctionsas_full-prjZ-mbp13
      AWS_DEFAULT_REGION:  (0.2) ap-northeast-1
      STEPF_STATEM_ARN:    (0.3) arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:stateMachine:ActivityStateMachine

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


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:変数の設定:
STEPF_STATEM_NAME='ActivityStateMachine'
```

```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:ActivityStateMachine


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


実行名の決定
------------

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


2. 実行
=======

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

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

ETX
```

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

結果(例):

      {
        "startDate": 1489733645.48,
        "executionArn": "arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:execution:ActivityStateMachine:act-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:ActivityStateMachine:act-1

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


結果(例):

      RUNNING


4. アプリケーションの実行
=========================


4.1. アプリケーションの実行
---------------------------


```bash:変数の設定:
FILE_OUTPUT="${STEPF_ACTIVITY_NAME}-output.json" \
        && echo ${FILE_OUTPUT}
```

```bash:コマンド:
aws stepfunctions get-activity-task \
        --activity-arn ${STEPF_ACTIVITY_ARN} \
        > ${FILE_OUTPUT}

      cat ${FILE_OUTPUT}
```

結果(例):

      {
        "input": "{}",
        "taskToken": "AAAAKgAAAAIAAAAAAAAAAReLVjAVqs2Gg42g+9hqziOm97b98bEbyrQfGkwf4IfHwqnTWTaaBttJwRl4Iud4CA8k5XF1zKfFuseDPbwUTOgirPREUeImgF8ch7acEX3kTRkM64kx7syUqviL27JCMHjixKoOkWMuG6ZRxpTKRRwwMqtmKZWtAUGclHfxEpY9NuHoCfcVqh9hQ9avXZGKPLKsLUwHWvFs4Nc6dR3GuaEmA1H40j55ICncbAUlNM2f7pYlObpk0NiTaBmP09nY+sq+aTSMHweWfdpWoHRKa5oiUYoiKxv5/P+0oyo+U7f0fuLMbmmLKvyBu1E6/f+AkHLXs19KA4aV9mtgkiBXq979SXjr8XnAGRUZOVmPMSQtDc/skTtcOW7Hox0JatCsPJntKDV06x3TV8R6Vra333gnto06QxTM5JtKHVLe28smEcymk6czmYpxqFHH7uR3AYJnyOh3g/xSUX6I+4L1izOc0jsQ3hQhNpZvvEhVVQ0MIWb4j2k2rZnzIa7sekijcA=="
      }


```bash:コマンド:
STEPF_TASK_TOKEN=$( \
        cat ${FILE_OUTPUT} \
          | jp.py 'taskToken' \
          | sed 's/\"//g' \
) \
        && echo ${STEPF_TASK_TOKEN}
```

結果(例):

      AAAAKgAAAAIAAAAAAAAAAUCZlSM75uOmd5+r2l6EKKamH28QZi22KrhBZ/Qxen21CftutQeJ0h5LA077tU3l8PThRznf5jBiEgLbzfVUJKcyfrTdPAXbZyDid0UbNvGZt+5pDt6DvMJ7i2wP/XPYDFMxInelW+Z1Eh9qHFY/up8OwgqaH2+Gxm+sRTBfsb52DtS1G+v7pWtWxQk9aJU1zIYH8HDyn0xjeSiIFPfqxtpS7E/Bzm6Y0VaojFdzXeyXgFrK+wNpNMnSqiL7Pwbck3I0ez54hkBkYTC6AkCFw+1UqfnmxM5I/N1l3PqQWNBKaXnZa0x7jj7jSkfD/yKGZ7XannVSXK5EpO27e392AhFeBavVwJiE5JPgIWT2F+AvjLnmSwODKIvYbqNeCXI8EwWwHSpmfTD26YFeXGIpN07QnBitDx9697k7SNRXli2yt/yEhj0Ro83A8+5VdWJetIqqZ4snlcrqkjHHG57AKSPCaE6yn6uFiD50A+CC8qSY9lN9eGsaKQQegT4+/7F6rA==

```bash:コマンド:
STEPF_TASK_INPUT=$( \
        cat ${FILE_OUTPUT} \
          | jp.py 'input' \
          | sed 's/\"//g' \
) \
        && echo ${STEPF_TASK_INPUT}
```

```bash:変数の設定:
STEPF_TASK_OUTPUT="${STEPF_TASK_INPUT}" \
        && echo ${STEPF_TASK_OUTPUT}
```


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

        STEPF_TASK_TOKEN:  ${STEPF_TASK_TOKEN}
        STEPF_TASK_OUTPUT: ${STEPF_TASK_OUTPUT}

ETX
```

```bash:コマンド:
aws stepfunctions send-task-success \
        --task-token ${STEPF_TASK_TOKEN} \
        --task-output ${STEPF_TASK_OUTPUT}
```

結果(例):

      >>|yet|<<


4.2. ステータスの確認
---------------------

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


結果(例):

      SUCCEEDED


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

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


結果(例):

      {}


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

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

結果(例):

      {
        "events": [
          {
              "timestamp": 1489912589.005,
              "executionStartedEventDetails": {
                  "input": "{}",
                  "roleArn": "arn:aws:iam::XXXXXXXXXXXX:role/StepFunctionsRole-ap-northeast-1"
              },
              "type": "ExecutionStarted",
              "id": 1,
              "previousEventId": 0
          },
          {
              "timestamp": 1489912589.047,
              "type": "TaskStateEntered",
              "id": 2,
              "stateEnteredEventDetails": {
                  "input": "{}",
                  "name": "getGreeting"
              },
              "previousEventId": 0
          },
          {
              "timestamp": 1489912589.047,
              "activityScheduledEventDetails": {
                  "input": "{}",
                  "resource": "arn:aws:states:ap-northeast-1:XXXXXXXXXXXX:activity:get-greeting"
              },
              "type": "ActivityScheduled",
              "id": 3,
              "previousEventId": 2
          },
          {
              "timestamp": 1489912606.402,
              "activityStartedEventDetails": {
                  "workerName": "testWorker"
              },
              "type": "ActivityStarted",
              "id": 4,
              "previousEventId": 3
          },
          {
              "activitySucceededEventDetails": {
                  "output": "{}"
              },
              "timestamp": 1489912606.538,
              "type": "ActivitySucceeded",
              "id": 5,
              "previousEventId": 4
          },
          {
              "timestamp": 1489912606.538,
              "stateExitedEventDetails": {
                  "output": "{}",
                  "name": "getGreeting"
              },
              "type": "TaskStateExited",
              "id": 6,
              "previousEventId": 5
          },
          {
              "executionSucceededEventDetails": {
                  "output": "{}"
              },
              "timestamp": 1489912606.538,
              "type": "ExecutionSucceeded",
              "id": 7,
              "previousEventId": 6
          }
        ]
      }


完了
====