Checking Why Your Route53 Domain and CloudFront Distribution Is Not Working
Amazon Web Services (AWS) provide a robust ecosystem for managing various applications and data across scalable cloud servers. This guide will help you troubleshoot why your Route53 Domain and Cloudfront Distribution may not be working as expected. We will use JavaScript and specifically Node.js AWS SDK.
Code
const AWS = require('aws-sdk'); const route53 = new AWS.Route53(); const cloudFront = new AWS.CloudFront(); async function getRoute53AndCloudfrontDistributionInfo() { const hostedZones = await route53.listHostedZonesByName().promise(); const distributions = await cloudFront.listDistributions().promise(); const distribution = distributions.DistributionList.Items.find(distribution => distribution.Aliases.Items.includes('example.com')); console.log(distribution); } getRoute53AndCloudfrontDistributionInfo();
Detailed Code Explanation
First, the script imports AWS and initialize two objects - route53
and cloudFront
to make calls to Route53 and CloudFront services respectively.
Next, it creates an asynchronous function getRoute53AndCloudfrontDistributionInfo()
that fetches all route53 hosted zones and Cloudfront distributions your AWS account has access to and then it finds the distribution with the alias of 'example.com'.
Finally the script logs the distribution whose alias is 'example.com'.
Expected Output Format
The expected output is a CloudFront distribution in a JSON format.
{ "CFDistributionConfig": { "CallerReference": "ExampleReference", "Aliases": { "Items": ["example.com"], "Quantity": 1 }, "DefaultRootObject": "index.html", "Origins": { // Information about the origin of your resources. }, "LastModifiedTime": "2021-08-15T16:00:00.000Z", "Id": "EDFDVBD6EXAMPLE" } }
Considerations & Caveats
This script only addresses only one distribution with the alias 'example.com'. If you have other distributions or/testing environments that aren't functioning it's recommended to extend this script or replicate it for those specific distributions.
Make sure you handle any exceptions that might occur during execution to prevent your script from crashing and to make tracking issues easier.
Required IAM Permissions and Example Policy
The script requires two specific IAM policy permissions: route53:ListHostedZonesByName
and cloudfront:ListDistributions
. You need to make sure the IAM role used has these permissions.
Here is an example policy:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "route53:ListHostedZonesByName", "cloudfront:ListDistributions" ], "Resource": "*" } ] }
FAQ
-
Why is the script not working? Make sure your IAM role has the correct permissions as mentioned above and you have AWS SDK correctly installed and configured in your environment.
-
Why didn't my distribution get logged to the console? Check the alias you're using. In this case, we're using 'example.com'.
-
Can I modify this script to get more information? Yes. AWS SDK allows you to do pretty much anything programmatically that you can do in the AWS management console.
-
I get a lot of hosted zones or distributions back. How do I limit the returned results? You can modify the
MaxItems
field in thelistHostedZonesByName()
andlistDistributions()
methods to limit the results returned.