Created
April 26, 2017 15:34
-
-
Save dividedharmony/2d50f92b3a462117a0fa655190ab31d0 to your computer and use it in GitHub Desktop.
A Shell Script for listing the Domain names of your CloudFront Distributions and their Origins
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'json' | |
####### | |
# | |
# Classes to help execution | |
# | |
####### | |
class CloudfrontDomainList | |
attr_reader :distributions | |
def initialize(raw_json_string) | |
json = JSON.parse(raw_json_string) | |
@distributions = json.dig('DistributionList', 'Items') | |
end | |
def domain_listing | |
distributions.map do |distribution_json| | |
CloudfrontDomain.new(distribution_json).log_to_console | |
end | |
end | |
end | |
class CloudfrontDomain | |
attr_reader :distribution_json | |
def initialize(distribution_json) | |
@distribution_json = distribution_json | |
end | |
def log_to_console | |
$stdout.puts "Domain Name: #{domain_name}\t------\tOrigins: #{origins_string}" | |
end | |
private | |
def domain_name | |
distribution_json['DomainName'] | |
end | |
def origins_string | |
origins = distribution_json.dig('Origins', 'Items') | |
origins.map { |origin| origin['DomainName'] }.join("\t") | |
end | |
end | |
####### | |
# | |
# Line of execution | |
# | |
####### | |
CloudfrontDomainList.new(`aws cloudfront list-distributions`).domain_listing |
I started pretty simple, but you could also add, at the very least, the status of each distribution to the printout.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE: This shell script rests on several assumptions:
Before you run this for the first time:
Run
aws configure set preview.cloudfront true
as (at time of writing) thelist-distributions
command is still in preview mode.If you'd like to use a different aws cli profile:
Simply switch the command on line 55 from
to
where
your_preferred_profile
is the name of the profile you'd like to use