AWS CloudFormation
User Guide (API Version 2010-05-15)
AWS 服务或AWS文档中描述的功能,可能因地区/位置而异。请点击 Amazon AWS 入门,可查看中国地区的具体差异

AWS::ApiGateway::GatewayResponse

AWS::ApiGateway::GatewayResponse 资源可创建 API 网关 API 的自定义响应。有关更多信息,请参阅 API 网关 开发人员指南 中的 API Gateway 响应

语法

要在 AWS CloudFormation 模板中声明此实体,请使用以下语法:

JSON

{ "Type" : "AWS::ApiGateway::GatewayResponse", "Properties" : { "ResponseParameters" : { String:String, ... }, "ResponseTemplates" : { String:String, ... }, "ResponseType" : String, "RestApiId" : String, "StatusCode" : String } }

YAML

Type: "AWS::ApiGateway::GatewayResponse" Properties: ResponseParameters: String: String ResponseTemplates: String: String ResponseType: String RestApiId: String StatusCode: String

属性

ResponseParameters

用于响应的响应参数 (路径、查询字符串和标头)。不允许重复项。

必需:否

类型:字符串到字符串映射

更新要求无需中断

ResponseTemplates

用于响应的响应模板。不允许重复项。

必需:否

类型:字符串到字符串映射

更新要求无需中断

ResponseType

响应类型。要查看有效值,请参阅 API 网关 API 参考 中的 GatewayResponse

必需:是

类型:字符串

更新要求替换

RestApiId

目标 API 实体的标识符。

必需:是

类型:字符串

更新要求替换

StatusCode

响应的 HTTP 状态代码。

必需:否

类型:字符串

更新要求无需中断

示例

404 响应

以下示例对未找到的资源,而不是对 CORS 请求缺失的身份验证令牌返回 404 状态代码 (适用于不安全/不受限制的 API)。

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources": { "RestApi": { "Type": "AWS::ApiGateway::RestApi", "Properties": { "Name": "myRestApi" } }, "GatewayResponse": { "Type": "AWS::ApiGateway::GatewayResponse", "Properties": { "ResponseParameters": { "gatewayresponse.header.Access-Control-Allow-Origin": "'*'", "gatewayresponse.header.Access-Control-Allow-Headers": "'*'" }, "ResponseType": "MISSING_AUTHENTICATION_TOKEN", "RestApiId": { "Ref": "RestApi" }, "StatusCode": "404" } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: RestApi: Type: 'AWS::ApiGateway::RestApi' Properties: Name: myRestApi GatewayResponse: Type: 'AWS::ApiGateway::GatewayResponse' Properties: ResponseParameters: gatewayresponse.header.Access-Control-Allow-Origin: "'*'" gatewayresponse.header.Access-Control-Allow-Headers: "'*'" ResponseType: MISSING_AUTHENTICATION_TOKEN RestApiId: !Ref RestApi StatusCode: '404'

参数化响应

以下示例基于提供的参数为 API 创建响应。

JSON

{ "Parameters": { "apiName": { "Type": "String" }, "responseParameter1": { "Type": "String" }, "responseParameter2": { "Type": "String" }, "responseType": { "Type": "String" }, "statusCode": { "Type": "String" } }, "Resources": { "RestApi": { "Type": "AWS::ApiGateway::RestApi", "Properties": { "Name": { "Ref": "apiName" } } }, "GatewayResponse": { "Type": "AWS::ApiGateway::GatewayResponse", "Properties": { "ResponseParameters": { "gatewayresponse.header.k1": { "Ref": "responseParameter1" }, "gatewayresponse.header.k2": { "Ref": "responseParameter2" } }, "ResponseType": { "Ref": "responseType" }, "RestApiId": { "Ref": "RestApi" }, "StatusCode": { "Ref": "statusCode" } } } } }

YAML

Parameters: apiName : Type : String responseParameter1: Type : String responseParameter2: Type : String responseType: Type : String statusCode: Type : String Resources : RestApi: Type: AWS::ApiGateway::RestApi Properties: Name: !Ref apiName GatewayResponse: Type: AWS::ApiGateway::GatewayResponse Properties: ResponseParameters: gatewayresponse.header.k1 : !Ref responseParameter1 gatewayresponse.header.k2 : !Ref responseParameter2 ResponseType: !Ref responseType RestApiId: !Ref RestApi StatusCode: !Ref statusCode

本页内容: