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

API Gateway でレスポンスストリーミング(CDK)

Posted at

はじめに

2025/11から,API Gatewayがレスポンスストリーミングに対応しました。
この記事は,CDKでこの機能を試すためのメモです。

注意点

すでにPRが出ているので,CDKが正式に対応したらそちらを使うことが良いです。

やりかた

L2 Construct からCfnMethodを取り出して,以下2つを上書きします。

  • Properties.Integration.ResponseTransferMode
    • レスポンス転送モードがSTREAMであることを指定
  • Properties.Integration.Uri
const lambda: Function = ... // 統合するLambda(L2 Construct)
const api: LambdaRestApi = ... // API Gateway(L2 Construct)

const cfnMethod = api.node.findChild("Default").node.findChild("ANY").node.findChild("Resource") as CfnMethod;
const cfnFunction = lambda.node.defaultChild as CfnFunction;
const integrationProps = cfnMethod.integration as any as IntegrationProps;

cfnMethod.addOverride("Properties.Integration.ResponseTransferMode", "STREAM");
cfnMethod.addOverride("Properties.Integration.Uri",
  Fn.join('', [
    'arn:aws:apigateway:',
    this.region,
    ':lambda:path/2021-11-15/functions/',
    cfnFunction.attrArn,
    '/response-streaming-invocations'
  ]),
);

終わりに

独自ドメインでレスポンスストリーミングに対応させようとすると,これまでは以下の構成が必要でした:

  • CloudFront (+OAC認証)
  • Lambda Function Url

この構成だと,CloudFrontに使わせるACM証明書をus-east-1リージョンで作成する必要があります。国内リージョンとus-east-1リージョンへのデプロイが必要で,まあまあ面倒です。

一方 API Gatewayであれば,アタッチするACM証明書のリージョンに縛りはありません。
小規模なデプロイやちょっとした検証が,とても便利になりました。

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