Created
January 28, 2017 15:31
-
-
Save nickrobinson/4361117eb34b56f5130762e4bdf95d83 to your computer and use it in GitHub Desktop.
Route53 Dynamic DNS
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' | |
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 |
Author
nickrobinson
commented
Jan 28, 2017
- 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