在 Cloudformation 模板中为 API 网关启用 CORS DEFAULT 4XX/5XX

Iro*_*255 3 cors aws-cloudformation api-gateway

我正在为我的环境创建 AWS Cloudformation 模板,并尝试为 API 网关方法启用 CORS。问题#40292888 的回答链接到问题#40292888部分回答了我的问题。当 API 返回代码 200 时,解决方案效果很好,但我仍然收到CORS 标头 \xe2\x80\x9cAccess-Control-Allow-Origin\xe2\x80\x9d 丢失而没有提供返回代码 403 Forbidden 的 api-key。我知道,如果您在从控制台启用 CORS 时检查 DEFAULT 4XX/5XX 它可以工作,我将如何在我的 cloudformation 模板中模拟它?

\n

默认 4XX/5XX Api 网关控制台

\n

谢谢你,

\n

Iro*_*255 6

我自己找到了答案

通过控制台启用 CORS 时检查 DEFAULT 4XX/5XX 时,它会在 API 的网关响应下填充响应标头 key:value。

以下是在 CloudFormation 模板中进行模拟的代码(5xx 重复)。

GatewayResponses4xx:
Type: AWS::ApiGateway::GatewayResponse
Properties: 
  ResponseParameters: 
    gatewayresponse.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
    gatewayresponse.header.Access-Control-Allow-Methods: "'GET,OPTIONS'"
    gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
  ResponseType: DEFAULT_4XX
  RestApiId: !Ref BWTAPI
  # StatusCode: String
Run Code Online (Sandbox Code Playgroud)

谢谢。