在Cloudformation模板中为API网关启用CORS

时间:2016-10-27 19:36:20

标签: amazon-web-services cors amazon-cloudformation aws-api-gateway

我正在为我的环境创建AWS Cloudformation模板,但我无法找到为API网关方法启用CORS的方法。

我可以使用AWS控制台(here is the official doc)对其进行配置,但是如何在Cloudformation模板中进行配置?

5 个答案:

答案 0 :(得分:35)

经过一些试验和错误后,我发现与CORS控制台向导相比,以下CloudFormation模板代码段将生成等效的OPTIONS方法:

OptionsMethod:
  Type: AWS::ApiGateway::Method
  Properties:
    AuthorizationType: NONE
    RestApiId:
      Ref: MyApi
    ResourceId:
      Ref: MyResourceOnWhichToEnableCORS
    HttpMethod: OPTIONS
    Integration:
      IntegrationResponses:
      - 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,OPTIONS'"
          method.response.header.Access-Control-Allow-Origin: "'*'"
        ResponseTemplates:
          application/json: ''
      PassthroughBehavior: WHEN_NO_MATCH
      RequestTemplates:
        application/json: '{"statusCode": 200}'
      Type: MOCK
    MethodResponses:
    - StatusCode: 200
      ResponseModels:
        application/json: 'Empty'
      ResponseParameters:
          method.response.header.Access-Control-Allow-Headers: false
          method.response.header.Access-Control-Allow-Methods: false
          method.response.header.Access-Control-Allow-Origin: false

*注1 :这是获取POST默认值的示例。显然,您需要更新Access-Control-Allow-Methods以包含所需的值。

* Note 2 :对最近推出YAML支持的AWS CloudFormation团队表示感谢。如果你需要转换为/从YAML / JSON转换,我发现这个网站很方便:http://www.json2yaml.com/

答案 1 :(得分:2)

API网关对自动CORS配置的支持目前仅适用于API网关控制台。您仍然可以在从swagger导入API或通过CloudFormation定义API时自己设置CORS,但是您必须指定用于设置OPTIONS方法的所有参数以及将CORS特定标头添加到其他方法。

This page显示了在导入swagger时如何设置CORS。通过CloudFormation设置CORS在概念上类似,但使用CloudFormation语法而不是swagger语法。

答案 2 :(得分:1)

它仅创建选项方法,仍然需要对GET,POST等方法进行响应, 我已经创建了一个完整的hello world cloudformation

https://github.com/seraphjiang/aws-cors-cloudformation/tree/master

答案 3 :(得分:0)

试试这个:

  OPTIONS: 
   Type: AWS::ApiGateway::Method 
   Properties: ApiKeyRequired: false
   RestApiId: !Ref YourAPI 
   ResourceId: !Ref YourResourceName 
   HttpMethod: OPTIONS 
   AuthorizationType: NONE 
   Integration: 
    Type: MOCK 
    IntegrationResponses: 
     - 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: "'GET,OPTIONS'" 
      method.response.header.Access-Control-Allow-Origin: "'*'" 
     ResponseTemplates: 
      application/json: '' 
    PassthroughBehavior: WHEN_NO_MATCH 
    RequestTemplates: 
     application/json: '{"statusCode": 200}' 
    Type: MOCK 
   MethodResponses: 
   - StatusCode: 200 
   ResponseModels: 
    application/json: 'Empty' 
   ResponseParameters: 
    method.response.header.Access-Control-Allow-Headers: false 
    method.response.header.Access-Control-Allow-Methods: false 
    method.response.header.Access-Control-Allow-Origin: false

答案 4 :(得分:0)

此代码段已用于我的团队的部署。请注意,这是具有ANY方法的代理资源。

CORSOptionsMethod: # Adds cors
    Type: "AWS::ApiGateway::Method"
    Properties:
      ResourceId:
        !Ref apiProxy
      RestApiId:
        !Ref api
      AuthorizationType: NONE
      HttpMethod: OPTIONS
      Integration:
        Type: MOCK
        IntegrationResponses:
          - ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,Cache-Control'"
              method.response.header.Access-Control-Allow-Methods: "'GET,POST,PUT,DELETE,OPTIONS'"
              method.response.header.Access-Control-Allow-Origin: !Sub
                - "'${CORSOrigin}'"
                - { 'CORSOrigin': !FindInMap [Environment, !Ref Environment, CORSOrigin] }
            ResponseTemplates:
              application/json: ''
            StatusCode: '200'
        PassthroughBehavior: NEVER
        RequestTemplates:
          application/json: '{"statusCode": 200}'
      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'