Skip to content

Instantly share code, notes, and snippets.

@dividedharmony
Created April 26, 2017 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dividedharmony/2d50f92b3a462117a0fa655190ab31d0 to your computer and use it in GitHub Desktop.
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
#!/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
@dividedharmony
Copy link
Author

dividedharmony commented Apr 26, 2017

NOTE: This shell script rests on several assumptions:

  1. You have ruby 2.0+ installed on your system
  2. You have an AWS account
  3. You have installed the AWS CLI
  4. Your default AWS CLI profile has permission to view CloudFront distributions

Before you run this for the first time:
Run aws configure set preview.cloudfront true as (at time of writing) the list-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

CloudfrontDomainList.new(`aws cloudfront list-distributions`).domain_listing

to

CloudfrontDomainList.new(`aws cloudfront list-distributions --profile your_preferred_profile`).domain_listing

where your_preferred_profile is the name of the profile you'd like to use

@dividedharmony
Copy link
Author

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