Skip to content

Instantly share code, notes, and snippets.

@prein
Last active August 3, 2016 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save prein/3b36b66fdc1c4cc440c8afe64157f35d to your computer and use it in GitHub Desktop.
Save prein/3b36b66fdc1c4cc440c8afe64157f35d to your computer and use it in GitHub Desktop.
Copy DNS zones from CloudFlare to Route53
require 'rubyflare'
require 'route53'
require 'yaml'
credentials = YAML.load(File.read("credentials.yml"))
cloudflare_conn = Rubyflare.connect_with(credentials['cloudflare']['email'], credentials['cloudflare']['api_key'])
r53_conn = Route53::Connection.new(credentials['aws']['access_key'],credentials['aws']['secret_key'])
# Get all zones (domains)
cf_zones = cloudflare_conn.get('zones', { per_page: 1000 })
r53_zones = r53_conn.get_zones()
cloudfront_distribution_name = "foo1234567890.cloudfront.net"
cloudfront_distribution_id = "ABCDEFGHIJKLM10"
# Iterate over zones, and their records and create them in r53
cf_zones.results.each do |cf_zone|
# sleeps are there to prevent throttling, poor mans way
sleep 1
mx_content = Hash.new {|h,k| h[k] = [] }
# Check if the zone exists already
puts "checking if #{cf_zone[:name]} zone exists in r53 - will create it only if it doesn't"
# r53_zone = Array(r53_conn.get_zones(cf_zone[:name])).first
unless r53_zones.find {|r53_zone| r53_zone.name == "#{cf_zone[:name]}."}
r53_zone = Route53::Zone.new("#{cf_zone[:name]}.",nil,r53_conn)
# Create a zone in route53
puts "creating new zone in r53: #{cf_zone[:name]}."
resp = r53_zone.create_zone
exit 1 if resp.error?
while resp.pending?
sleep 1
end
# get the zone records
r53_zone_records = r53_zone.get_records
cf_zone_dns_records = cloudflare_conn.get("zones/#{cf_zone[:id]}/dns_records", { per_page: 1000 })
cf_zone_dns_records.results.each do |cf_record|
sleep 1
case cf_record[:type]
when "A"
puts "processing A record in #{cf_zone[:name]} zone, name #{cf_record[:name]} content #{cf_record[:content]}"
case cf_record[:name]
when "www.#{cf_zone[:name]}"
cf_record[:type] = "CNAME"
cf_record[:content] = "#{cf_zone[:name]}"
when "#{cf_zone[:name]}"
cf_record[:content] = cloudfront_distribution_name
end
# Check if it doesn't exist in Route53 yet
puts "checking if #{cf_record[:name]} #{cf_record[:type]} record exists in r53 - will create it only if it doesn't"
unless r53_zone_records.find {|r53_record| r53_record.name == "#{cf_record[:name]}." && r53_record.type == cf_record[:type] && r53_record.values.include?(cf_record[:content])}
puts "creating #{cf_record[:type]} record named #{cf_record[:name]} in #{cf_zone[:name]} zone with content #{cf_record[:content]}"
#Create a new record within our newly created r53 zone.
if cf_record[:content] == cloudfront_distribution_name
new_record = Route53::DNSRecord.new("#{cf_record[:name]}.",cf_record[:type],"60",["#{cf_record[:content]}"],r53_zone,cloudfront_distribution_id)
else
new_record = Route53::DNSRecord.new("#{cf_record[:name]}.",cf_record[:type],"60",["#{cf_record[:content]}"],r53_zone)
end
new_record.create
end
when "SOA", "NS"
next
when "MX"
mx_content[cf_record[:name]] << "#{cf_record[:priority]} #{cf_record[:content]}."
else
# Check if it doesn't exist in Route53 yet
puts "checking if #{cf_record[:name]} #{cf_record[:type]} record exists in r53 - will create it only if it doesn't"
unless r53_zone_records.find {|r53_record| r53_record.name == "#{cf_record[:name]}." && r53_record.type == cf_record[:type] && r53_record.values.include?(cf_record[:content])}
puts "creating #{cf_record[:type]} record named #{cf_record[:name]} in #{cf_zone[:name]} zone with content #{cf_record[:content]}"
#Create a new record within our newly created r53 zone.
new_record = Route53::DNSRecord.new("#{cf_record[:name]}.",cf_record[:type],"60",["#{cf_record[:content]}"],r53_zone)
new_record.create
end
end
end
end
mx_content.each do |mx_record_name,mx_record_content|
# Check if it doesn't exist in Route53 yet
puts "checking if #{mx_record_name} MX record exists in r53 - will create it only if it doesn't"
unless r53_zone_records.find {|r53_record| r53_record.name == "#{mx_record_name}." && r53_record.type == "MX" && r53_record.values == mx_record_content.uniq}
puts "creating MX record named #{mx_record_name} with in #{cf_zone[:name]} zone with content #{mx_record_content}"
#Create a new MX record within our newly created r53 zone.
new_mx_record = Route53::DNSRecord.new("#{mx_record_name}.","MX","60",mx_record_content.uniq,r53_zone)
new_mx_record.create
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment