如何在两个CloudFront发行版之间设置CORS?

时间:2019-07-23 15:50:41

标签: aws-lambda cors amazon-cloudfront api-gateway

我有两个CloudFront发行版:

  • 有人将S3用作起点-route53域-example.com
  • second使用API​​网关URL作为源。 -route53域-api.com

JS代码托管在S3和First Cloudfront Distribution上。 https://example.com/test.html正在致电https://api.com/api?<queryParameters>

但是我收到错误消息: Access to XMLHttpRequest at 'https://api.com/api?<queryParameters>' from origin 'https://example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

我尝试在API CloudFront Behavior的Access-Control-Request-Headers的白名单中添加Access-Control-Request-MethodOriginCache Based on Selected Request Headers。还是没有运气

1 个答案:

答案 0 :(得分:0)

通过在Lambda函数响应中添加Header消息解决了此问题。 与此类似:

   response["headers"] = {
       'Content-Type': 'application/json', 
       'Access-Control-Allow-Origin': '*' 
   }

参考:https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-cors.html