AWSServerless FW

serverless-step-functionsでネストしたワークフローを実行する

Step FunctionsからStep Fuctionsを実行するネストワークフローをServerless Frameworkで設定する方法についての覚書です。 Note:バージョンに注意 GitHubのIssueで可 […]

広告ここから
広告ここまで

Step FunctionsからStep Fuctionsを実行するネストワークフローをServerless Frameworkで設定する方法についての覚書です。

Note:バージョンに注意

GitHubのIssueで可能になったことを知ったパターンですが、古いバージョンを使い続けているとこの方法は使えません。Issue のやりとりを読み返す限りだと、2.14.0以降である必要がありそうです。

Can you try again against the latest v2.14.0? It now supports one IAM role per state machine and should fix the circular dependency you saw before

https://github.com/horike37/serverless-step-functions/issues/273#issuecomment-568403855

sls deployで以下のようなエラーが出た時は、高確率でバージョンが古いですのでpackage.jsonを確認しましょう。

  Error --------------------------------------------------
 
  Error: The CloudFormation template is invalid: Circular dependency between resources: [IamRoleStateMachineExecution, ChildStateMachineStepFunctionsStateMachine, YourCatchMachineStepFunctionsStateMachine, ParentStateMachineStepFunctionsStateMachine]

簡単なStep Functionsを2つ作成した例

簡単なサンプルを用意しました。

service:
  name: nested-step-functions

plugins:
  - serverless-step-functions

provider:
  name: aws
  runtime: nodejs10.x
  logRetentionInDays: 14
  region: us-east-1

# 呼び出すLambdaは便宜上1つにする
functions:
  test:
    handler: handler.test

stepFunctions:
  stateMachines:
    # 親から呼ばれる側のState Machine
    ChildStateMachine:
      definition:
        Comment: "Child state machine"
        StartAt: Hello
        States:
          Hello:
            Type: Task
            Resource:
              Fn::GetAtt: [test, Arn]
            End: true

    # ChildStateMachineを呼び出すState Machine
    ParentStateMachine:
      definition:
        Comment: "Parent state machine"
        StartAt: HelloKids
        States:
          HelloKids:
            Type: Task
            Resource:
              Fn::GetAtt: [test, Arn]
            Next: CallKids
          CallKids:
            Type: Task
            Resource: "arn:aws:states:::states:startExecution.sync"
            Parameters:
              StateMachineArn:
                Ref: ChildStateMachineStepFunctionsStateMachine
              Input:
                AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$: "$$.Execution.Id"
            End: true

パラメーターについての参考記事: https://dev.classmethod.jp/cloud/aws/step-functions-update-nest/

StateMachineを呼びだす部分について

StateのTypeはLambda同様にTaskを指定します。Resourcesで特定の文字列を設定し、Parametersに対象のARNやExecution IDなどを渡すように設定してやりましょう。

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts