如何在不设置 lambda 的情况下在 apigateway http_proxy 上添加 CORS?

How can I add CORS on apigateway http_proxy without setting a lambda?

我有一个 API 网关,网关中定义了一些路径。我想将 CORS 启用为 http_proxy 指向后端中的 appsync 的路径之一。 Appsync 解析器指向一个 dynamodb table。所以前端调用 API 网关,网关将请求传递给 appsync,请求由 dynamodb 解析。

我的问题是在该特定路径上启用 CORS。但是基于此文档:https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html 它说我必须在后端响应 CORS header,这是一个 lambda。但是我在后端没有 lambda。所有后端都只是 appsync + 一些解析器(例如 Dynamodb)。如何让它支持 CORS?

当我直接调用 appsync 端点时,它响应 access-control-allow-origin: * 并且它在前端(网站)上工作正常。但是,如果前端调用 API 网关域,它将因为 cors 而被拒绝。我相信 apigateway 不允许 CORS 那么如何在没有后端 lambda 的情况下在 http_proxy 集成中启用 cors?

假设您有以下 CloudFormation 声明用于基于 GraphQLHTTP_PROXY 集成 POST 方法

GraphQLPostMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: POST
      Rest of the properties for HTTP_PROXY integration

OPTIONS 请求再定义一种方法。如果源即 AppSync 使用适当的 headers 响应选项请求,您应该可以使用 HTTP_PROXY 集成。如果没有或者您不想将选项请求发送到 AppSync,在这种情况下,您可以使用选项方法的 MOCK 集成。

GraphQLOptionsMethod:
    Type: AWS::ApiGateway::Method
    Properties:
      AuthorizationType: NONE
      HttpMethod: OPTIONS
      ResourceId: !Ref <ReplaceWithYourResrouce>
      RestApiId: !Ref <ReplaceWithYourAPI>
      MethodResponses:
        - ResponseModels:
            application/json: Empty
          ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: true
            method.response.header.Access-Control-Allow-Methods: true
            method.response.header.Access-Control-Allow-Origin: true
          StatusCode: '200'
      Integration:
        Type: MOCK
        IntegrationResponses:
          - ResponseTemplates:
              application/json: ''
            StatusCode: '200'
            ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
              method.response.header.Access-Control-Allow-Methods: "'POST'"
              method.response.header.Access-Control-Allow-Origin: !Sub "'${ReplaceWithYourCORSAllowedOrigin}' or hardcode to *"
        PassthroughBehavior: NEVER
        RequestTemplates:
          application/json: '{"statusCode": 200}'