LoginSignup
0
0

More than 1 year has passed since last update.

[AWS] Step FunctionsのoutputPath、resultPath、resultSelectorを使いこなす

Posted at

はじめに

AWS Step Functions の各タスクへの入力は、inputPathでJSON pathを指定することで、雑多な入力から必要な項目だけを拾うことができます。

出力側にはoutputPath、resultPath、resultSelectorというものがありますが、どうにも動作がわからなかったので調べてみようと思い立ちました。

と、勢い込んでみたものの、AWS Console にData flow simulator というよいものがありました。この記事読むより、シミュレーターで試してみたほうがよいです。

でも、めげずに書いてみます。

動作を確認した環境

Lambda

入力と出力を確かめたいだけなので、値を返すだけのLambdaを作りました。これをStep Functionsの中で呼び出してみます。

export const sampleLambdaHandler = async (event: unknown) => {
  console.log(event);
  return { event, bonsoir: "lambda" };
};

Step Functions

Step Functions はCDKで作りました。最初にPassタスクで、JSONを渡すようにしています。

const parameters = {
  hello: "world",
  array: ["1", "2", "3", "4", "5"],
};
const definition = new Pass(this, "pass json", { parameters })
.next( /* Lambda 呼び出し */ );

const stateMachine = new StateMachine(this, "sample", {
  definition,
});

渡しているJSONだけを抜き出すとこんなんです。

{
  hello: "world",
  array: ["1", "2", "3", "4", "5"],
}

試したこと

色々指定して試してみます。

何も指定しない場合

new LambdaInvoke(this, "lambda 1", {
    lambdaFunction,
})

出力

戻り値が Payload キーに格納されました。他にも色々出力されています。

image.png

outputPath を指定した場合

new LambdaInvoke(this, "lambda 1", {
    lambdaFunction,
    outputPath: "$.Payload",
})

出力

Payload キーの内容だけが出力されました。

image.png

resultPath を指定した場合

new LambdaInvoke(this, "lambda 1", {
    lambdaFunction,
    resultPath: "$.MyResult"
})

出力

もともとの出力に、MyResult キーが追加されました。
MyResult キーの値は、もともとの出力です。

image.png

JSONで書くとこんな感じです。

{A: original}

{A: original, MyResult: {A: original}}

resultSelector を指定した場合

new LambdaInvoke(this, "lambda 1", {
    lambdaFunction,
    resultSelector: {
      "hello.$": "$.Payload.bonsoir",
      "bonsoir.$": "$.Payload.event.hello",
    },
})

出力

指定したキーに、指定した値が出力されました。

image.png

まとめ

outputPath、resultPath、resultSelector の役割がわかりました。

そしてやっぱり Data flow simulator 使うほうが理解が早そう。

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