LoginSignup
13
12

More than 3 years have passed since last update.

StepFunctionsの入力方法サンプルメモ集

Last updated at Posted at 2019-12-04

メモ

前提

検証用Lambdaファンクション

引数を出力するだけ

require 'json'

def lambda_handler(event:, context:)
    puts event
    puts context
    { statusCode: 200, body: JSON.generate('Hello from Lambda!') }
end

InputPath

StepFunctionsの実行の入力のうちで、Lambdaファンクションにわたす引数を抽出する。

ステートマシン定義

{
  "Comment": "test",
  "StartAt": "Hello",
  "States": {
    "Hello": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-northeast-1:xxx:function:inputparameter-test",
      "InputPath": "$.num1",
      "End": true
    }
  }
}

StepFunctions実行入力

{
  "num1": 1,
  "num2": 2
}

Lambdaファンクション出力(event)

$.num1 と指定することで、入力のうちの num1 キーの値だけが event引数で渡される

1

Parameters

Lambdaファンクションにわたす引数を定義する。

ステートマシン定義

{
  "Comment": "test",
  "StartAt": "Hello",
  "States": {
    "Hello": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-northeast-1:xxx:function:inputparameter-test",
      "Parameters": {
        "Param1": 1,
        "Param2": 2,
        "Param3": "hogehoge"
      },
      "End": true
    }
  }
}

StepFunctions実行入力

{
  "num1": 1,
  "num2": 2
}

Lambdaファンクション出力(event)

Parameters の中身がそのまま入ってくる

{"Param1"=>1, "Param2"=>2, "Param3"=>"hogehoge"}

Parameters(入力値を利用、パス指定)

パスを使用して値を選択するキーと値のペアの場合、キーの名前は .$ で終わる必要があります。
https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/input-output-inputpath-params.html

ステートマシン定義

{
  "Comment": "test",
  "StartAt": "Hello",
  "States": {
    "Hello": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-northeast-1:xxxx:function:inputparameter-test",
      "Parameters": {
        "Num1.$": "$.num1",
        "Num2": "$.num2",
        "NumAll.$": "$"
      },
      "End": true
    }
  }
}

StepFunctions実行入力

{
  "num1": 1,
  "num2": 2
}

Lambdaファンクション出力(event)

キーが .$ で終わってない場合は固定文字列として扱われる。

{
"Num1"=>1, 
"Num2"=>"$.num2"
"NumAll"=>{"num1"=>1, "num2"=>2}
}

Parameters(コンテキストオブジェクト)

コンテキストオブジェクトは、実行中に使用できる内部の JSON 構造です。これには、ステート定義の "Parameters" フィールド内からアクセスできるステートマシンと実行に関する情報が含まれています。これにより、ワークフローが特定の実行に関する情報にアクセスできるようになります。
(略)
コンテキストオブジェクトにアクセスするには、パスを使用して状態の入力を選択したときと同様に、.$ を末尾に追加したパラメータ名をまず指定します。次に、入力の代わりにコンテキストオブジェクトデータにアクセスするには、 $$. をパスの先頭に追加します。これによって、パスを使用してコンテキストオブジェクト内のノードを選択するように Step Functions に指示します。
https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/input-output-contextobject.html

ステートマシン定義

{
  "Comment": "test",
  "StartAt": "Hello",
  "States": {
    "Hello": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-northeast-1:xxxxx:function:inputparameter-test",
      "Parameters": {
        "ContextObject.$": "$$"
      },
      "End": true
    }
  }
}

StepFunctions実行入力

{
  "num1": 1,
  "num2": 2
}

Lambdaファンクション出力(event)

コンテキストオブジェクトの形式
https://docs.aws.amazon.com/ja_jp/step-functions/latest/dg/input-output-contextobject.html

{"ContextObject"=>{"Execution"=>{"Id"=>"arn:aws:states:ap-northeast-1:xxxxx:execution:input-parameter-sample:6026cee0-cd11-3527-42e3-03d40b886310", "Input"=>{"num1"=>1, "num2"=>2}, "Name"=>"6026cee0-cd11-3527-42e3-xxxxxxx", "RoleArn"=>"arn:aws:iam::xxxxxx:role/service-role/2019-12-042311", "StartTime"=>"2019-12-04T14:45:55.068Z"}, "StateMachine"=>{"Id"=>"arn:aws:states:ap-northeast-1:xxxxx:stateMachine:input-parameter-sample", "Name"=>"input-parameter-sample"}, "State"=>{"Name"=>"Hello", "EnteredTime"=>"2019-12-04T14:45:55.104Z", "RetryCount"=>0}}}
13
12
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
13
12