0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2017-03-20

前提条件

StepFunctionsへの権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.11.57
コマンド
aws --version

結果(例):

  aws-cli/1.11.34 Python/2.7.10 Darwin/15.6.0 botocore/1.4.91

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

コマンド
sudo -H pip install -U awscli
  1. 準備
    =======

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

変数の確認
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:LambdaStateMachine

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

0.1. プロファイルの指定

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

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

結果(例):

  iamFull-prjz-mbpr13
  stepfunctionsas_full-prjZ-mbp13
変数の設定
export AWS_DEFAULT_PROFILE='stepfunctionsas_full-prjZ-mbp13'

0.2. リージョンの指定

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

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

変数の設定
DIR_CONF='conf-stepfunctions'
コマンド
mkdir -p ${DIR_CONF}

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

変数の設定
STEPF_STATEM_NAME='LambdaStateMachine'
コマンド
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:LambdaStateMachine
  1. 事前作業
    ===========

1.1. 実行名の決定

変数の設定
STEPF_EXEC_NAME='lambda-1'

1.2. 入力の指定

変数の設定
FILE_INPUT="${DIR_CONF}/stepfunctions-exec-${STEPF_EXEC_NAME}.json" \
        && echo ${FILE_INPUT}
コマンド
cat << EOF > ${FILE_INPUT}
{
        "who" : "AWS Step Functions"
}
EOF

cat ${FILE_INPUT}

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

コマンド
jsonlint -q ${FILE_INPUT}

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

  1. 実行
    =======
変数の確認
cat << ETX

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

ETX
コマンド
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:LambdaStateMachine:lambda-1"
  }
  1. 事後作業
    ===========
コマンド
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:LambdaStateMachine:lambda-1
コマンド
STEPF_EXEC_STATUS=$( \
        aws stepfunctions describe-execution \
          --execution-arn ${STEPF_EXEC_ARN} \
          --query 'status' \
          --output text \
) \
        && echo ${STEPF_EXEC_STATUS}

結果(例):

  SUCCEEDED

完了

アウトプットの確認

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

結果(例):

  "Hello, AWS Step Functions!"

実行ヒストリの確認

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

結果(例):

  {
    "events": [
      {
          "timestamp": 1489839841.46,
          "executionStartedEventDetails": {
              "input": "{\n  \"who\" : \"AWS Step Functions\"\n}\n",
              "roleArn": "arn:aws:iam::XXXXXXXXXXXX:role/StepFunctionsRole-ap-northeast-1"
          },
          "type": "ExecutionStarted",
          "id": 1,
          "previousEventId": 0
      },
      {
          "timestamp": 1489839841.507,
          "type": "TaskStateEntered",
          "id": 2,
          "stateEnteredEventDetails": {
              "input": "{\n  \"who\" : \"AWS Step Functions\"\n}\n",
              "name": "HelloWorld"
          },
          "previousEventId": 0
      },
      {
          "timestamp": 1489839841.507,
          "lambdaFunctionScheduledEventDetails": {
              "input": "{\n  \"who\" : \"AWS Step Functions\"\n}\n",
              "resource": "arn:aws:lambda:ap-northeast-1:XXXXXXXXXXXX:function:HelloFunction"
          },
          "type": "LambdaFunctionScheduled",
          "id": 3,
          "previousEventId": 2
      },
      {
          "timestamp": 1489839842.291,
          "type": "LambdaFunctionStarted",
          "id": 4,
          "previousEventId": 3
      },
      {
          "lambdaFunctionSucceededEventDetails": {
              "output": "\"Hello, AWS Step Functions!\""
          },
          "timestamp": 1489839842.587,
          "type": "LambdaFunctionSucceeded",
          "id": 5,
          "previousEventId": 4
      },
      {
          "timestamp": 1489839842.587,
          "stateExitedEventDetails": {
              "output": "\"Hello, AWS Step Functions!\"",
              "name": "HelloWorld"
          },
          "type": "TaskStateExited",
          "id": 6,
          "previousEventId": 5
      },
      {
          "executionSucceededEventDetails": {
              "output": "\"Hello, AWS Step Functions!\""
          },
          "timestamp": 1489839842.587,
          "type": "ExecutionSucceeded",
          "id": 7,
          "previousEventId": 6
      }
    ]
  }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?