AWS Step Functionsステートマシンからステートマシンを実行する際に両実行を紐付けてトレースしやすくする(AWS CDK v2)

2022.06.13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

今回は、AWS Step Functionsステートマシンからステートマシンを実行する際に両実行を紐づけてトレースしやすくしてみました。

やってみた

実装

AWS CDK v2(TypeScript)で2つのステートマシンリソースの作成を行います。次のようなCDKスタックで実装をします。

lib/process-stack.ts

import { Construct } from 'constructs';
import {
  aws_stepfunctions,
  aws_stepfunctions_tasks,
  Stack,
  StackProps,
  Duration,
} from 'aws-cdk-lib';

export class ProcessStack extends Stack {
  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    // ステートマシン(実行される側)
    const stateMachine2 = new aws_stepfunctions.StateMachine(
      this,
      'stateMachine2',
      {
        stateMachineName: 'stateMachine2',
        definition: new aws_stepfunctions.Wait(this, 'wait10seconds', {
          time: aws_stepfunctions.WaitTime.duration(Duration.seconds(10)),
        }),
      },
    );

    // ステートマシン実行タスク
    const executeStateMachineTask =
      new aws_stepfunctions_tasks.StepFunctionsStartExecution(
        this,
        'executeStateMachineTask',
        {
          stateMachine: stateMachine2,
          associateWithParent: true,
        },
      );

    // ステートマシン(実行する側)
    new aws_stepfunctions.StateMachine(this, 'stateMachine1', {
      stateMachineName: 'stateMachine1',
      definition: executeStateMachineTask,
    });
  }
}

CDK Deployしてリソースを作成します。

実行する側のステートマシン(stateMachine1)のDefinitionは次のようになりました。ここで着目すべきはハイライト行の記載です。

{
  "StartAt": "executeStateMachineTask",
  "States": {
    "executeStateMachineTask": {
      "End": true,
      "Type": "Task",
      "Resource": "arn:aws:states:::states:startExecution",
      "Parameters": {
        "Input": {
          "AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id"
        },
        "StateMachineArn": "arn:aws:states:ap-northeast-1:xxxxxxxxxxxx:stateMachine:stateMachine2"
      }
    }
  }
}

associateWithParentオプションを有効にすると、実行時のInputに"AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID.$": "$$.Execution.Id"が追加されるようにます。これによりステートマシンの実行が紐付けされて実行間のトレースが行いやすくなります。

You can use a special parameter named AWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_ID when you start an execution. If included, this association provides links in the Step details section of the Step Functions console. When provided, you can easily trace the executions of your workflows from starting executions to their started workflow executions.

動作確認

stateMachine1を実行します。

実行が成功しました。この時実行履歴で実行されたステートマシンの情報を参照できるリンクメニューが表示されるようになります。(これは紐付けの有無に関わらず表示されます。)

[Execution]を開くと、実行されたstateMachine2の実行詳細画面を開けました。そして[Started By]で実行元のstateMachine1の実行のリンクメニューが表示され、こちらからも参照ができるようになっています!

リンクを開くと実行元の実行の詳細画面を開けました!

また実行された側の

紐付けをしなかった場合

ここでassociateWithParentfalseにして紐付けを無効にして、紐付けがされないことも確認してみます。

lib/process-stack.ts

    // ステートマシン実行タスク
    const executeStateMachineTask =
      new aws_stepfunctions_tasks.StepFunctionsStartExecution(
        this,
        'executeStateMachineTask',
        {
          stateMachine: stateMachine2,
          //associateWithParent: true,
        },
      );

変更をCDK DeployしたらstateMachine1を再度実行します。

実行されたstateMachine2側の実行を見ると、[Started By]メニューが表示されなくなっています。

[Execution input & output]メニューを見ても、InputにAWS_STEP_FUNCTIONS_STARTED_BY_EXECUTION_IDは追加されていませんね。

おわりに

AWS Step Functionsステートマシンからステートマシンを実行する際に両実行を紐づけてトレースしやすくしてみました。

ステートマシンが実行された時のInputに前述のフィールドが追加されて問題なければ、とりあえず紐付けを有効にすると良さそうです。

参考

以上