Search code examples
amazon-web-servicesurl-routingamazon-cloudfrontamazon-route53nameservers

Can't access subdomain website hosted in S3 behind CloudFront


I own a domain example.com, and want to host a static website in an S3 bucket on a subdomain of this, subdomain.example.com.

I can view the website through the S3 static website endpoint and through the CloudFront domain endpoint but not when I go to my subdomain.

I have an S3 bucket called subdomain.example.com which contains an index.html file. It is a publicly-accessible bucket and when I go to the bucket properties, I can see a link, http://subdomain.example.com.s3-website.eu-west-2.amazonaws.com, under "Static Website Hosting". Clicking this link allows me to view the site.

I have a CloudFront Distribution for this site configured as follows. Under "Alternate Domain Names" I have subdomain.example.com and when I go the CloudFront domain name, dXXXXXXXXXXXX.cloudfront.net, I can view the site. This leads me to believe that there's some problem in the Route53 side of things.

I had previously registered the domain, example.com, in Route53 and it has the name servers:

  • ns-1.awsdns-1.co.uk,
  • ns-2.awsdns-2.com,
  • ns-3.awsdns-3.org,
  • ns-4.awsdns-4.net. I ensured that the hosted zone for example.com had the same name servers.

I created a hosted zone for subdomain.example.com and it had the following name servers:

  • ns-5.awsdns-5.co.uk,
  • ns-6.awsdns-6.com,
  • ns-7.awsdns-7.org,
  • ns-8.awsdns-8.net.

I then added an NS record into the hosted zone for example.com with the aim of routing traffic to the name servers for subdomain.example.uk:

  • Name: subdomain.example.uk
  • Type: NS
  • Routing Policy: Simple
  • (Alias: No - can't be changed)
  • Value/Route Traffic to: ns-5.awsdns-5.co.uk,ns-6.awsdns-6.com,ns-7.awsdns-7.org,ns-8.awsdns-8.net
  • TTL: 86400

Lastly, I added an A and AAAA record into the hosted zone for subdomain.example.com with the aim of routing traffic to the CloudFront distribution:

  • Name: subdomain.example.uk
  • Type: A
  • Routing Policy: Simple
  • Alias: Yes
  • Value/Route Traffic to: Alias to CloudFront Distribution - my distribution appears and was selected

I'm expecting that when I go to subdomain.example.com I'll see my website, but this doesn't happen.

I've tried waiting for over an hour as CloudFront can take up to 25 minutes. I've tried clearing the cache in my browser/using a different device. After research, the common resolutions seem to be verifying that the hosted zone for the domain contains an NS record pointing to the name servers of the subdomain, and verifying that distribution was referenced in the Alias record.

I've read guides such as this CNAME guide and this CloudFront guide but I believe I've configured everything as described, or can't see what I'm missing. I'm having a similar problem to this question, but am not getting an access denied error, rather an NS_ERROR_UKNOWN_HOST when I view the Network tab in developer tools.

It may be worth noting that I configured CloudFront to redirect HTTP requests to HTTPS and created a public certificate for *.subdomain.example.com and subdomain.example.com. When creating this, a CNAME record was added to subdomain.example.com as well.


Solution

  • To troubleshoot, I tried changing the name servers of my registered domain to match those of my subdomain hosted zone but it wasn't working so I left it for 8+ hours and then noticed it was working.

    After changing the name servers back to the original ones to match my domain hosted zone and waiting 8+ hours, it's working as intended.

    It appears I just needed to wait for longer.