Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Replace second bucket and CloudFront distribution with Lambda function #2

Open
awkspace opened this issue May 22, 2019 · 2 comments

Comments

@awkspace
Copy link

A Lambda@Edge deployment would, I think, be a more lightweight solution for redirection than another bucket and distribution.

Something like (written for !Sub):

exports.handler = async (event, context) => {
    
    const request = event.Records[0].cf.request;
    const headers = request.headers;
    const host = headers.host[0].value.toLowerCase();

    if (host == 'www.${APEX_DOMAIN}') {
        /*
         * Generate HTTP redirect response with 302 status code and Location header.
         */
        const response = {
            status: '302',
            statusDescription: 'Found',
            headers: {
                location: [{
                    key: 'Location',
                    value: 'https://${APEX_DOMAIN}',
                }],
            },
        };
        return response;
    } else {
        return request;
    }

};
@tschoffelen
Copy link

How about the cost of this at scale? The current S3 option would be much cheaper, right?

@celioreyes
Copy link

We are using Lambda@Edge at my workplace and it scales pretty well.

One thing to take note of though is where you run the Lambda@Edge function. You only get the real Host header when you run the function in the ViewerRequest hook which will not be cached. When you execute on the OriginRequest hook, which is cached, it will replace the Host header with the origin's Host header ( For an S3 origin the Host header would be example-bucket.s3-website-region.amazonaws.com instead of example-bucket.com)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants