Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 417
David Alsh Avatar asked Jul 21 '26 10:07

David Alsh


1 Answers

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.

like image 142
Mrk Fldig Avatar answered Jul 24 '26 03:07

Mrk Fldig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!