Last active
August 29, 2015 14:17
-
-
Save ibejohn818/382214ea6acd9419d60d 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 php | |
<?php | |
$template = [ | |
"Comment"=>"", | |
"Changes"=>[ | |
["Action"=>"#action#", | |
"ResourceRecordSet"=>[ | |
"ResourceRecords"=>[ | |
["Value"=>"#value#"] | |
], | |
"Name"=>"#recordset#", | |
"Type"=>"A", | |
"TTL"=>"#ttl#" | |
] | |
] | |
] | |
]; | |
//to hold id's | |
$hostedZones = []; | |
$cnameZones = []; | |
$hosts = [ | |
"johnchardy.com"=>[ | |
"homelab.johnchardy.com"=>"", | |
"homefiles.johnchardy.com"=>"", | |
"homeserver.johnchardy.com"=>"" | |
] | |
]; | |
//get IP | |
$ipaddr = trim(`curl -sS https://wtfismyip.com/text`); | |
//get my hosts zones | |
$hZones = json_decode(`aws route53 list-hosted-zones --output json`); | |
foreach($hZones->HostedZones as $k=>$zone) { | |
$name = rtrim($zone->Name,"."); | |
if(array_key_exists($name,$hosts)) { | |
$hosts[$name]['id'] = $zone->Id; | |
} | |
} | |
//chop off unlisted zones | |
foreach($hosts as $k=>$host) { | |
if(!array_key_exists("id",$host)) { | |
unset($hosts[$k]); | |
} | |
} | |
//determine resource actions | |
foreach($hosts as $k=>$host) { | |
$res = json_decode(`aws route53 list-resource-record-sets --hosted-zone-id {$host['id']}`,true); | |
foreach($host as $cname=>$val) { | |
if($cname == "id") { | |
continue; | |
} | |
foreach($res['ResourceRecordSets'] as $set) { | |
if(rtrim($set['Name'],".") == $cname) { | |
$hosts[$k][$cname] = "UPSERT"; | |
} | |
} | |
} | |
} | |
//update route53 | |
foreach($hosts as $host=>$cnames) { | |
$id = $cnames['id']; | |
foreach($cnames as $cname=>$action) { | |
if($cname == "id") { | |
continue; | |
} | |
if(strlen($action)<=0) { | |
$template['Changes'][0]['Action'] = "CREATE"; | |
} else { | |
$template['Changes'][0]['Action'] = "UPSERT"; | |
} | |
$template['Changes'][0]['ResourceRecordSet']['ResourceRecords'][0]['Value'] = $ipaddr; | |
$template['Changes'][0]['ResourceRecordSet']['Name'] = $cname; | |
$template['Changes'][0]['ResourceRecordSet']['TTL'] = 300; | |
$json = json_encode($template); | |
$tmpFile = "/tmp/{$cname}.json"; | |
file_put_contents($tmpFile,$json); | |
passthru("aws route53 change-resource-record-sets --hosted-zone-id {$id} --change-batch file://{$tmpFile}"); | |
unlink($tmpFile); | |
} | |
} | |
print_r($hosts); | |
echo $ipaddr; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment