はじめに
2025/11から,API Gatewayがレスポンスストリーミングに対応しました。
この記事は,CDKでこの機能を試すためのメモです。
注意点
すでにPRが出ているので,CDKが正式に対応したらそちらを使うことが良いです。
やりかた
L2 Construct からCfnMethodを取り出して,以下2つを上書きします。
- Properties.Integration.ResponseTransferMode
- レスポンス転送モードがSTREAMであることを指定
- Properties.Integration.Uri
- Lambda の呼び出しが InvokeWithResponseStream形式であることを指定
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証明書のリージョンに縛りはありません。
小規模なデプロイやちょっとした検証が,とても便利になりました。