
こんな悩みを解決できる記事を書きました!
僕は現役フリーランスエンジニア(歴9年)で、資格は13個保有しています。
結論、API GatewayのゲートウェイレスポンスをCloudFormationで設定するのは非常に簡単です。
具体的なソースコードを載せているので安心してくださいね。


本記事を読めば、API GatewayのゲートウェイレスポンスをCloudFormationで設定する方法が理解できるので早速見ていきましょう。



すぐ読み終わるので、ぜひ最後まで読んでくださいね♪
ゲートウェイレスポンスをCloudFormationで設定する方法
早速ですが、ゲートウェイレスポンスをCloudFormationで設定する方法を解説します。
AWSTemplateFormatVersion: 2010-09-09
Resources:
RestApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: hello-world
GatewayResponse:
Type: AWS::ApiGateway::GatewayResponse
Properties:
ResponseParameters:
gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
ResponseType: EXPIRED_TOKEN
RestApiId: !Ref RestApi
StatusCode: '403'
yamlファイルを用意します。



今回は、hello-worldというAPI Gatewayにゲートウェイレスポンスの設定を追加します。
EXPIRED_TOKEN(期限切れのトークン)のレスポンスヘッダに「Access-Control-Allow-Origin」を設定します。


CloudFormationを開き、スタックの作成をクリックします。


先ほど作成したyamlファイルをアップロードし、次へをクリックします。


スタックの名前を入力し、次へをクリックします。


デフォルトのまま、次へをクリックします。


デフォルトのまま、送信をクリックします。


スタックのステータスが「CREATE_COMPLETE」になればOKです。


API Gatewayのゲートウェイレスポンスにレスポンスヘッダが設定されていれば完了です。



簡単でしたね。
ゲートウェイレスポンスのレスポンスタイプ
ゲートウェイレスポンスのレスポンスタイプ(responseType)は以下になります。
- DEFAULT_4XX
- DEFAULT_5XX
- RESOURCE_NOT_FOUND
- UNAUTHORIZED
- INVALID_API_KEY
- ACCESS_DENIED
- AUTHORIZER_FAILURE
- AUTHORIZER_CONFIGURATION_ERROR
- INVALID_SIGNATURE
- EXPIRED_TOKEN
- MISSING_AUTHENTICATION_TOKEN
- INTEGRATION_FAILURE
- INTEGRATION_TIMEOUT
- API_CONFIGURATION_ERROR
- UNSUPPORTED_MEDIA_TYPE
- BAD_REQUEST_PARAMETERS
- BAD_REQUEST_BODY
- REQUEST_TOO_LARGE
- THROTTLED
- QUOTA_EXCEEDED
よくある質問
GatewayResponseの公式ドキュメントはありますか?
下記になります。
まとめ
今回は、API GatewayのゲートウェイレスポンスをCloudFormationで設定する方法について解説しました。
以下が本記事のまとめになります。



最後までお読みいただき、ありがとうございました!
- クソおすすめ本



海外のエンジニアがどういった思考で働いているかが理解できます。
海外に行く気はないけど海外エンジニアの動向が気になる雑魚エンジニアにおすすめです(本当におすすめな本しか紹介しないのでご安心を)。