1
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 1 year has passed since last update.

AWS Step Functions でオプション引数を使いたい

Last updated at Posted at 2021-12-06

はじめに

本記事では、AWS StepFunctionsでオプション引数の機能を実現する方法をまとめました。

negocia株式会社について

negocia株式会社では3つのValueを行動指針とし、「うれしい広告」の実現を目指しています。

Value

  • Professional Pride プロであり続ける
  • Praise the Challenge 挑戦を楽しむ
  • Ownership for All 全てにオーナシップをもつ

興味を持たれた方は採用情報をご覧ください。

やりたいこと

このPythonコードのように、AWS StepFunctionsでオプション引数を使いたい。

>>> def say_hello(name="World"):
...     return f"Hello, {name}"
... 
>>> say_hello()
'Hello, World'
>>> say_hello("negocia")
'Hello, negocia'

手段

StepFunctionsChoicePassを使う。
Choiceで引数の有無を調べ、Passでデフォルト値を補う。

StepFunctions の定義

{
  "StartAt": "HasName",
  "States": {
    "HasName": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.name",
          "IsPresent": false,
          "Next": "FillName"
        }
      ],
      "Default": "Say"
    },
    "FillName": {
      "Type": "Pass",
      "Result": "World",
      "ResultPath": "$.name",
      "Next": "Say"
    },
    "Say": {
      "Type": "Pass",
      "Parameters": {
        "message.$": "States.Format('Hello, {}', $.name)"
      },
      "OutputPath": "$.message",
      "End": true
    }
  }
}

実行例

引数を与えなかった場合

  • 入力{}
  • 出力"Hello, World"

stepfunctions_graph.png

引数を与えた場合

  • 入力{"name": "negocia"}
  • 出力"Hello, negocia"

stepfunctions_graph(1).png

参考

1
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
1
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?