[AWS CDK] aws_stepfunctions.StateMachine コンストラクトクラスの definition プロパティが deprecated となりました

今後は definitionBody プロパティを使いましょう
2023.07.12

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

今回は、AWS CDK の aws_stepfunctions.StateMachine コンストラクトクラスの definition プロパティが deprecated となった件についてです。

StateMachineProps#definition が deprecated となる

次の CDK の記述で Step Functions ワークフローを構成しようとしました。

lib/cdk-sample-stack.ts

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

export class CdkSampleStack extends Stack {
  public readonly myFileObjectKey: string;

  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    new aws_stepfunctions.StateMachine(this, 'MyStateMachine', {
      definition: new aws_stepfunctions.Pass(this, 'MyPassState', {}),
    });
  }
}

しかし何かしらの CDK コマンドを実行すると、以下のように Warning が出るようになりました。

$ cdk synth
[WARNING] aws-cdk-lib.aws_stepfunctions.StateMachineProps#definition is deprecated.
  use definitionBody: DefinitionBody.fromChainable()
  This API will be removed in the next major release.
[WARNING] aws-cdk-lib.aws_stepfunctions.StateMachineProps#definition is deprecated.
  use definitionBody: DefinitionBody.fromChainable()
  This API will be removed in the next major release.
...

調査

メッセージにあった通りなのですが、definition プロパティ は deprecated となり、代わりに definitionBody プロパティを使うようにします。

ちなみにThis API will be removed in the next major release. ともありますが、「ext major release」というのは AWS CDK v3 を指しているので当面削除はされないようです。

definitionBody で指定する DefinitionBody クラスの仕様は下記で確認できます。

DefinitionBody クラスでは次の3種類のメソッドでワークフローの定義を指定できます。

  • fromChainable(chainable)
  • fromFile(path, options)
  • fromString(definition)

fromFile および fromString に関しては次の記事で紹介した通り、CDK v2.85.0 で新しく実装されたメソッドです。

CDK v2.85.0 の Pull Request を改めて見てみると、definition の deprecated 化および definitionBody の追加も同時に行われていました。

ワークフロー定義を指定できるプロパティが2種類あるのは好ましくないので、definitionBody に統一を図るための処置だというのは納得です。

対処

と言うわけで definitionBody プロパティを指定するように修正します。

lib/cdk-sample-stack.ts

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

export class CdkSampleStack extends Stack {
  public readonly myFileObjectKey: string;

  constructor(scope: Construct, id: string, props: StackProps) {
    super(scope, id, props);

    new aws_stepfunctions.StateMachine(this, 'MyStateMachine', {
      definitionBody: aws_stepfunctions.DefinitionBody.fromChainable(
        new aws_stepfunctions.Pass(this, 'MyPassState', {})
      ),
    });
  }
}

すると CDK コマンド実行時に Warning は出なくなりました。

おわりに

AWS CDK の aws_stepfunctions.StateMachine コンストラクトクラスの definition プロパティが deprecated となった件についてでした。

CDK を v2.85.0 以降にアップグレードした際は、definition プロパティを definitionBody プロパティに置き換えるように修正しましょう。

以上