Last active
August 29, 2015 13:59
-
-
Save einyx/d45bcd4c472cd93e01ef to your computer and use it in GitHub Desktop.
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 'aws-sdk' | |
require 'net/http' | |
AWS.config({}) | |
hostname = ARGV[0].to_s | |
domain = 'i.domain.com' | |
zone = '' | |
ttl = 60 | |
metadata_endpoint = 'http://169.254.169.254/latest/meta-data/' | |
hostname_local = Net::HTTP.get( URI.parse( metadata_endpoint + 'local-hostname' ) ) | |
hostname_public = Net::HTTP.get( URI.parse( metadata_endpoint + 'public-hostname' ) ) | |
records = [{ | |
:alias => [ hostname, domain, '' ] * '.', | |
:target => hostname_local | |
},{ | |
:alias => [ hostname + '-public', domain, '' ] * '.', | |
:target => hostname_public | |
}] | |
#update DNS records | |
rrsets = AWS::Route53::HostedZone.new(zone).rrsets | |
records.each{ |record| | |
rrset = rrsets[ | |
record[ :alias ], | |
'CNAME' | |
] | |
if rrset.exists? | |
rrset.delete | |
end | |
rrset = rrsets.create( | |
record[ :alias ], | |
'CNAME', | |
:ttl => ttl, | |
:resource_records => [ | |
{ :value => record[ :target ] } | |
] | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment