Amazon Web Services (AWS) CloudFront Setup

The following screenshots illustrate how to configure AWS CloudFront to deliver content from an AEM origin.

Create a cache policy

In the CloudFront sidebar, go to Policies → Cache → Create cache policy.

Click Create.

Create an origin request policy

In the CloudFront sidebar, go to Policies → Origin request → Create origin request policy.

Click Create.

Create a CloudFront distribution

In the navigation pane, choose DistributionsCreate distribution.


Configure the origin (custom headers)

Open your distribution → Origins tab → select the origin → Edit.

Under Add custom header, add the following headers:

If you have successfully configured push invalidation for your project you should also add the following custom header:

Save changes.

Enable invalidations by cache tag

Open your distribution → General tab → Edit.

Choose Use cache tags for cache invalidation. Leave the default for Header to use for cache tags: x-amz-meta-cache-tag

Save changes.

Create a Function to remove Age and X-Robots-Tag headers

In the CloudFront sidebar, select Functions and click Create function.

Enter a name for the function (e.g. stripHeaders), an optional description, set the Runtime to cloudfront-js-2.0, and click Create function.

In the Build tab, replace the code of the function with the following snippet and click Save changes:

function handler(event) {
    const response = event.response;
    const request = event.request;
    const headers = response.headers;

    // Strip age header
    delete headers['age'];

    // Check if the request URL does not end with '.plain.html'
    if (!request.uri.endsWith('.plain.html')) {
        delete headers['x-robots-tag'];
    }

    return response;
}

Select the Publish tab and click Publish function.

Finally, associate the function with your distribution by scrolling down to Associated distributions and click Add association.

In the following dialog, select:

Finally, click Add association.

That’s all (more or less). Please test the distribution in a stage environment.

Optional: Authenticate Origin Requests

If you have enabled token-based Site Authentication, go back to CloudFrontDistributions<your distribution>Origins<your AEM origin>Edit.

Under Add custom header, select Add header and create a header Authorization with value token <your-auth-token>. Replace <your-auth-token> with the token value created through token-based Site Authentication (it starts with hlx_ as the header value).

This will ensure that all requests from the AWS CloudFront CDN to your AEM origin use the correct authorization.