Skip to content

Instantly share code, notes, and snippets.

@nickrobinson
Created January 28, 2017 15:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickrobinson/4361117eb34b56f5130762e4bdf95d83 to your computer and use it in GitHub Desktop.
Save nickrobinson/4361117eb34b56f5130762e4bdf95d83 to your computer and use it in GitHub Desktop.
Route53 Dynamic DNS
#!/usr/bin/env ruby
require 'aws-sdk'
require 'net/http'
require 'json'
REGION = 'us-east-1'
HOSTED_ZONE_ID = 'INSERT_HOSTED_ZONE_ID'
DOMAIN = 'INSERT_DOMAIN_NAME'
response = Net::HTTP.get('ipinfo.io', '/')
json = JSON.parse(response)
current_ip = json['ip']
route53 = Aws::Route53::Client.new(region: REGION)
resource = route53.list_resource_record_sets({hosted_zone_id: HOSTED_ZONE_ID, start_record_name: DOMAIN, max_items: 1})
route53_ip = resource.resource_record_sets[0].resource_records[0].value
unless route53_ip == current_ip
# Update record to point to current IP address
resource.resource_record_sets[0].resource_records[0].value = current_ip
response = route53.change_resource_record_sets({
hosted_zone_id: HOSTED_ZONE_ID,
change_batch: {
comment: "VpnIpUpdate",
changes: [
{
action: "UPSERT",
resource_record_set: resource.resource_record_sets[0]
}
]
}
})
end
@nickrobinson
Copy link
Author

  • Make sure you have ~/.aws/credentials populated with creds for an IAM user who has Route53 permissions enabled
  • Make sure your have the 'aws-sdk' gem installed on your system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment