How to use custom error page with cloudfront edge function

0

I have built a simple CloudFront + S3 web hosting site with the following edge function on the Viewer request. The code is as follows.

The purpose of the edge function is twofold: 1. When a specific path is requested and the current timestamp exceeds the specified datetime, display "close.html". 2. When a directory is requested, complement it with "/index.html" and display the resulting page.

I also want to display "/404.html" when a requested resource does not exist on the origin.

However, when I configured a custom error page on the distribution, the edge function wouldn't run. "/404.html" was shown without specifying an existing resource. I suspect this is because the custom error page logic is invoked before the edge function.

Is there a way to achieve both a custom error page and the edge function?

function handler(event) {
    var request = event.request;
    var uri = request.uri;
    var closeTime = new Date('2023-05-31T14:59:59.000Z').getTime();
    var now = new Date().getTime();
    var isClosed = false;
    var requestPath = '';
    var newurl = '';
    var host = 'XXXXX.com';
    
    if (uri.endsWith('somePath/') || uri.endsWith('somePath')) {
        if (now > closeTime) {
            newurl = `https://${host}/somePath/close.html`
        } else{
            newurl = `https://${host}/somePath/index.html`
        }
    }
    
    if (!newurl) {
        if (uri.endsWith('/')) {
            request.uri += 'index.html';
            newurl = request.uri;
        } else if (!uri.includes('.')) {
            request.uri += '/index.html';
            newurl = request.uri;
        }
    }
    if (newurl) {
            var response = {
            statusCode: 301,
            statusDescription: 'Moved Permanently',
            headers:
                { "location": { "value": newurl } }
            }
            return response;
    }

    return request;
}

eisa
asked 10 months ago994 views
1 Answer
0
Accepted Answer

Hi, on your 2 questions:

  1. Error page: it is all described here how to configure it: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GeneratingCustomErrorResponses.html#custom-error-pages-procedure

In particular, about the 404

If you configure CloudFront to return a custom error page for an HTTP status 
code but the custom error page isn’t available, CloudFront returns to the viewer
 the status code that CloudFront received from the origin that contains the custom 
error pages. For example, suppose your custom origin returns a 500 status code 
and you have configured CloudFront to get a custom error page for a 500 status 
code from an Amazon S3 bucket. However, someone accidentally deleted the custom 
error page from your bucket. CloudFront returns an HTTP 404 status code (Not Found)
 to the viewer that requested the object.
  1. Re. close.html and index.html, you have to use Lambda@Edge: see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-at-the-edge.html

For your purposes, you have 2 options: use Lambda@Edge whenCloudFront receives a response from the origin (origin response) or before CloudFront returns the response to the viewer (viewer response)

Look at this page for changing return code: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GeneratingCustomErrorResponses.html#custom-error-pages-response-code

Look at this page for plenty of example along the line your index.html use case: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-access-request-body-examples

Hope it helps, Didier

profile pictureAWS
EXPERT
answered 10 months ago
profile picture
EXPERT
reviewed 9 months ago
profile picture
EXPERT
reviewed 10 months ago
  • Hi Didier, thank you for the advice.

    I didn't think of using "origin response" or "viewer response" to change return code and contents in case of 404/403. So I will try writing lambda@edge code. "Edge function" is useful when to implement very simple requirement, but seems unsuitable for complex logic.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions