How do I resolve the "No 'Access-Control-Allow-Origin' header is present on the requested resource" error from CloudFront?
I'm getting the CORS error "No 'Access-Control-Allow-Origin'" on my requested resource in Amazon CloudFront. Why am I getting this and how can I resolve it?
"I'm getting the CORS error "No 'Access-Control-Allow-Origin'" on my requested resource in Amazon CloudFront. Why am I getting this and how can I resolve it?ResolutionConfirm the origin's cross-origin resource sharing (CORS) policy allows the origin to return the Access-Control-Allow-Origin headerNote: If you receive errors when running AWS Command Line Interface (AWS CLI) commands, make sure that you’re using the most recent AWS CLI version.Run the following command to confirm the origin server returns the Access-Control-Allow-Origin header. Replace example.com with the required origin header. Replace https://www.example.net/video/call/System.generateId.dwr with the URL of the resource that's returning the header error.curl -H "Origin: example.com" -v "https://www.example.net/video/call/System.generateId.dwr"If the CORS policy allows the origin server to return the Access-Control-Allow-Origin header, you see a response similar to the following:HTTP/1.1 200 OKServer: nginx/1.10.2Date: Mon, 01 May 2018 03:06:41 GMTContent-Type: text/htmlContent-Length: 3770Last-Modified: Thu, 16 Mar 2017 01:50:52 GMTConnection: keep-aliveETag: "58c9ef7c-eba"Access-Control-Allow-Origin:    example.comAccept-Ranges: bytesIf CORS headers are not returned in the response, then the origin server is not correctly setup for CORS. Set up a CORS policy on your custom origin or Amazon Simple Storage Service (Amazon S3) origin.Configure the CloudFront distribution to forward the appropriate headers to the origin serverAfter you set up a CORS policy on your origin server, configure your CloudFront distribution to forward the origin headers to the origin server. If your origin server is an Amazon S3 bucket, then configure your distribution to forward the following headers to Amazon S3:Access-Control-Request-HeadersAccess-Control-Request-MethodOriginTo forward the headers to the origin server, CloudFront has two pre-defined policies depending on your origin type: CORS-S3Origin and CORS-CustomOrigin.To add a pre-defined policy to your distribution:Open your distribution from the CloudFront console.Choose the Behaviors tab.Choose Create Behavior. Or, select an existing behavior, and then choose Edit.Under Cache key and origin requests, choose Cache policy and origin request policy. Then, for Origin request policy, choose CORS-S3Origin or CORS-CustomOrigin from the dropdown list. For more information, see Using the managed origin request policies.Note: To create your own cache policy instead, see Creating cache policies.Choose Create Behavior. Or, choose Save changes if you're editing an existing behavior.To forward the headers using a cache policy:Create a cache policy.Under Cache key settings, for Headers, select Include the following headers. From the Add header dropdown list, choose one of the headers required by your origin.Fill in the cache policy settings as required by the behavior that you're attaching the policy to.Attach the cache policy to the behavior of your CloudFront distribution.To forward the headers using legacy cache settings:Open your distribution from the CloudFront console.Choose the Behaviors tab.Choose Create Behavior. Or, select an existing behavior, and then choose Edit.Under Cache key and origin requests, select Legacy cache settings.In the Headers dropdown list, choose the headers required by your origin. Choose Add custom to add headers required by your origin that don't appear in dropdown list.Choose Create Behavior. Or, choose Save changes if you're editing an existing behavior.Note: Be sure also to forward the header as part of your client request to CloudFront, which CloudFront forwards to the origin.Configure the CloudFront distribution's cache behavior to allow the OPTIONS method for HTTP requestsIf you still see errors after updating your CORS policy and forwarding the appropriate headers, allow the OPTIONS HTTP method in your distribution's cache behavior. By default, CloudFront only allows the GET and HEAD methods. However, some web browsers can issue requests for the OPTIONS method. To turn on the OPTIONS method on your CloudFront distribution:Open your distribution from the CloudFront console.Choose the Behaviors tab.Choose Create Behavior. Or, select an existing behavior, and then choose Edit.For Allowed HTTP Methods, select GET, HEAD, OPTIONS.Choose Create Behavior. Or, choose Save changes if you're editing an existing behavior.Configure your CloudFront response policy to return the required Access-Control-Allow-Origin headersIf the origin server isn't accessible or can't be set up to return the appropriate CORS headers, configure a CloudFront to return the required CORS headers. To configure, create response headers policies:Open your distribution from the CloudFront console.Choose the Behaviors tab.Choose Create Behavior. Or, select an existing behavior, and then choose Edit.For Response headers policy:Select an existing response policy from the dropdown list.-or-Choose Create policy to create a new response headers policy . In the new policy, under Cross-origin resource sharing, turn on CORS.Fill in other settings as needed and choose Create policy.From the Create Behavior page, choose the policy you created from the dropdown list.Choose Create Behavior. Or, choose Save changes if you're editing an existing behavior.Note: CloudFront typically deploys changes to distributions within five minutes. After you edit your distribution, invalidate the cache to clear previously cached responses.Related informationConfiguring CloudFront to respect CORS settingsConfiguring cross-origin resource sharing (CORS)Using the managed response headers policiesAdd a cross-origin resource sharing (CORS) header to the responseFollow"