Add cross-origin resource sharing (CORS) header to the request - Amazon CloudFront
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Add cross-origin resource sharing (CORS) header to the request

The following viewer request function adds an Origin HTTP header to the request if the request doesn’t already contain this header. This header is part of cross-origin resource sharing (CORS). This example sets the header’s value to the value in the request’s Host header. For more information, see Origin on the MDN Web Docs website.

See this example on GitHub.

JavaScript runtime 2.0
async function handler(event) { const request = event.request; const headers = request.headers; const host = request.headers.host.value; // If origin header is missing, set it equal to the host header. if (!headers.origin) headers.origin = {value:`https://${host}`}; return request; }
JavaScript runtime 1.0
function handler(event) { var request = event.request; var headers = request.headers; var host = request.headers.host.value; // If origin header is missing, set it equal to the host header. if (!headers.origin) headers.origin = {value:`https://${host}`}; return request; }