AWS CDK による API Gateway の Step Functions 統合を簡単に構築できるクラス2つのご紹介

2023.11.07

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

AWS Step Functions は API Gateway REST API との統合が可能です。これにより Step Functions から AWS サービスを呼び出す API を簡単に構築することができます。

そしてこの API Gateway の Step Functions 統合を AWS CDK で構築する場合は、StepFunctionsIntegration および StepFunctionsRestApi の 2 つの便利なクラスがあります。

今回はこれらのクラスを API Gateway の Step Functions 統合を構築しつつ、どんな違いがあるのか確認してみました。

確認してみた

CDK コード

AWS CDK のコードでの使用方法はそれぞれ以下のようになります。

lib/cdk-sample-stack.ts

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

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

    const stateMachine = new aws_stepfunctions.StateMachine(
      this,
      'StateMachine',
      {
        definitionBody: aws_stepfunctions.DefinitionBody.fromChainable(
          new aws_stepfunctions.Pass(this, 'MyPassState', {
            parameters: {
              key1: 'val1',
              key2: aws_stepfunctions.JsonPath.stringAt('$'),
            },
          })
        ),
        stateMachineType: aws_stepfunctions.StateMachineType.EXPRESS,
      }
    );

    // StepFunctionsRestApi Class
    new aws_apigateway.StepFunctionsRestApi(this, 'StepFunctionsRestApi', {
      stateMachine,
    });

    // StepFunctionsIntegration Class
    const restApi = new aws_apigateway.RestApi(this, 'RestApi');
    restApi.root.addMethod(
      'GET',
      aws_apigateway.StepFunctionsIntegration.startExecution(stateMachine)
    );
    restApi.root
      .addResource('{id}')
      .addMethod(
        'POST',
        aws_apigateway.StepFunctionsIntegration.startExecution(stateMachine)
      );
  }
}

  • いずれも使用できるステートマシンタイプは EXPRESS のみ。
  • StepFunctionsRestApi クラスは、 Step Functions と統合された API を、aws_stepfunctions.StateMachine オブジェクトを指定するだけで作成できる。
  • StepFunctionsIntegration クラスは、addMethod において統合の 1 パターンとして使用する。
  • StepFunctionsRestApi の方がより抽象度高く利用できるが、メソッドはルートパスに ANY が追加されるのみで、 StepFunctionsIntegration は任意のメソッドに対して統合を設定できる。

動作確認

デプロイすると、統合リクエスト(Integration request)、統合レスポンス(Integration response)およびメソッドレスポンス(Method response)がいずれのクラスの場合でも設定されますが、設定内容に関しては同じとなります(ただし後述の通り変更は可能)。

  • StepFunctionsRestApi

  • StepFunctionsIntegration

設定されている Mapping Template が同じなので、いずれも body 、querystring および path のパラメータを Step Functions の入力として渡されます。

  • StepFunctionsRestApi

  • StepFunctionsIntegration

Mapping Template の詳細は以前に下記ブログで紹介しているため、ここでの詳細は割愛します。

StepFunctionsRestApi はデフォルトで設定されるメソッドレスポンスを無効化できる

両クラスともデフォルトでメソッドレスポンスに対して 200 、400 および 500 のレスポンスが設定されますが、StepFunctionsRestApi ではそれを無効化できるオプションが直近のアップデートで追加されました。

apigateway: add useDefaultMethodResponses property for StepFunctionsIntegrations (#27645) (22a3234), closes #27520

既定の設定は次のようになります。

StepFunctionsRestApi で useDefaultMethodResponses プロパティを false にします。

    new aws_apigateway.StepFunctionsRestApi(this, 'StepFunctionsRestApi', {
      stateMachine,
      useDefaultMethodResponses: false,
    });

すると、メソッドレスポンスにレスポンスが設定されなくなります。

Step Functions からのレスポンスを、メソッドレスポンスに上書きされたくない場合などに有用となりそうです。

おわりに

AWS CDK による API Gateway の Step Functions 統合を簡単に構築できるクラス 2 つをご紹介しました。

以上