CloudFront function to redirect domain to other url



I have a subdomain (let's call it and Ineed to redirect it to****. The subdomain is managed within the zone in Route 53. We do not have access to create a rewrite rule on to redirect incoming requests from to the /read URI (which would be the simple way to achieve this I suppose.

From the reading I've been doing, I've been led to believe the simplest way to achieve this would be to create a distribution for in CloudFront, and then associate a function to redirect incoming requests to

Here's the function I created:

function handler(event) {
    var request = event.request;
    var headers = request.headers;
    var mainDomain = "";
    var uri = read

    if ( !== mainDomain) {
        return {
            statusCode: 301,
            statusDescription: "Moved Permanently",
            headers: {
                location: { value: "https://" + mainDomain + uri },

    return request;

The function fails testing with the message "read missing in 5".

I tried adding an alias to the record in Route 53 to point to a CloudFront distribution, but that failed with error

Error occurred
Alias Target contains an invalid value. (InvalidChangeBatch 400: "" is not a valid hosted zone id. is not a valid encrypted identifier)

I was able to add an alias Cname to point to the distribution URL.

Not sure what to make of this. Am I going in the right direction ? What am I missing ?

Thanks !


Thanks to Bryant Pollard for the script solution. If I enter the distribution URL in a browser it works fine now. However, I can't seem to get the DNS entry in Route 53 to work. If I try to do an A record pointing to the distribution, I'm still getting the message

Error occurred
Alias Target contains an invalid value. (InvalidChangeBatch 400: "" is not a valid hosted zone id. is not a valid encrypted identifier)

I can only create a cname entry pointing to the CloudFront distribution URL, but when I do that and then browse to I get the following:

The request could not be satisfied.
Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.

Generated by cloudfront (CloudFront)
Request ID: k4gMqQ4N1pgZQQ0cmfbyPxnVxzr6SqRLvyZocm95NhOvHp5pBZTXaQ==

It seems to have something to do with the region in which my CloudFront distribution is created vs where my zone is hosted, but I can't find where the CloudFront distribution is created. My zone is hosted in US East (N. Virginia).

Getting a little bit frustrated with the whole thing... :-(

asked a year ago1437 views
2 Answers
Accepted Answer

I've reviewed and did see 1 issue with the above code. Try adding a slash to this line:

location: { value: "https://" + mainDomain + "/" + uri },

It follows the example documented by Amazon:

Hope this helps, if so please accept this answer

profile picture
answered a year ago

There is an example here with JavaScript that may help.

It’s redirecting to a different page.

You may be able to use this and modify for your requirement.

profile picture
answered a year ago

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