Can't enable CORS on Lambda + API Gateway + Cloudfront on the Cloudfront layer

强颜欢笑 提交于 2019-12-24 18:33:55

问题


I have deployed a lambda, which in itself returns an 'Access-Control-Allow-Origin': '*' header in it's response.

I call it from an API Gateway. When calling the API gateway URL, the CORS configuration is fine.

I placed CloudFront in front of the API, however now I am unable to access the API as the CORS configuration seems to be overridden by CloudFront, and I am not sure where to add custom headers in CloudFront.

How do you enable CORS inside CloudFront?


回答1:


Go into your CloudFront distribution, click behaviours, select each behaviour and click edit, you'll see the section titled whitelist headers. Select 'Access-Control-Request-Headers' and 'Access-Control-request-methods' and click Add, also add Origin(thanks Michael for the catch), bottom right click "yes, edit".

Edit: please see Michael's comment below regarding preventing cached responses.

Voila.



来源:https://stackoverflow.com/questions/51462564/cant-enable-cors-on-lambda-api-gateway-cloudfront-on-the-cloudfront-layer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!