Error Page Custom Configuration

In this section, you replace the default S3 error response with a custom error page served by CloudFront.

Check the default error response

  1. Open a browser and request a path that doesn’t exist on your CloudFront domain.
    • S3 returns a 403 Access Denied XML response because the object doesn’t exist.
    • By default, CloudFront caches error responses for 10 seconds.

CloudFront Lab

Upload the error page to S3

  1. Create a file named error.html with a text editor (for example Notepad on Windows or TextEdit on macOS), paste the content below, and save it with UTF-8 encoding.
<html lang="en">
  <body>
    <h1>CloudFront Lab</h1>
    <strong>Ops, this is a nice error page!</strong>
  </body>
</html>
  1. Open the Amazon S3 console, select the lab bucket, and choose Upload.

CloudFront Lab

  1. Choose Add files, select error.html, then choose Upload.

CloudFront Lab

  1. Wait until the Upload succeeded message appears, then choose Close.

CloudFront Lab

  1. Confirm that the bucket now contains both error.html and index.html.

CloudFront Lab

Configure custom error responses in CloudFront

  1. Open the CloudFront console, select your distribution, open the Error pages tab, and choose Create custom error response.

CloudFront Lab

  1. Configure the response for 403 errors:

    • HTTP error code: 403: Forbidden
    • Error caching minimum TTL: 60
    • Customize error response: Yes
    • Response page path: /error.html
    • HTTP Response code: 200: OK
    • Choose Create custom error response.

    When the origin returns a 403, CloudFront serves /error.html with status 200 and caches the response for at least 60 seconds.

CloudFront Lab

  1. The message Successfully created 403 error page appears and the distribution status changes to Deploying.

CloudFront Lab

  1. After the deployment finishes, request a non-existent path again. Wait at least 10 seconds or use a new random path so you don’t get the cached 403 response from the earlier test.
    • The browser shows the custom error page.
    • In the browser developer tools (Network tab), the status code is 200 OK, Server is AmazonS3, and X-Cache is Error from cloudfront.
    • If you still see the default error, check that the deployment has finished and that the settings match step 2.

CloudFront Lab

  1. Choose Create custom error response again and add a response for when CloudFront can’t reach the origin:

    • HTTP error code: 504: Gateway Timeout
    • Error caching minimum TTL: 5
    • Customize error response: Yes
    • Response page path: /error.html
    • HTTP Response code: 200: OK
    • Choose Create custom error response.

    A short TTL of 5 seconds lets CloudFront retry the origin soon after it recovers.

CloudFront Lab

  1. The Error pages tab now lists both responses: 403 and 504.

CloudFront Lab

Verify the website

  1. Open your CloudFront domain in the browser. The home page still loads normally, because custom error responses only apply when the origin returns an error.

CloudFront Lab