Route53 + RaspberryPi + Cron + PHP = lazy admin.
Jun 1, 2015 13:29 · 241 words · 2 minute read
Thanks to my recently AWS-connected Pi, performing a scheduled DNS cutover from the comfort of my own bed could not have been easier.
With a little cron magic and some “Aws\Route53\Route53Client” you can easily schedule changes to Route53 records / record sets.
<?php
error\_reporting(E\_ALL);
ini\_set("Display Errors", 1);
require 'vendor/autoload.php';
// Create client object for Route53
$r53Client \= \\Aws\\Route53\\Route53Client::factory(array());
// Create client object for SES
$SesClient \= \\Aws\\Ses\\SesClient::factory(array(
'region' \=> 'us-east-1'
));
// Function for sending notifications if the record change fails or for confirmation that the change has been made.
function StackNotification($body, $cnameDns)
{
global $SesClient;
$stackSubject \= 'DNS Update Confirmation ' . "\[$cnameDns\]";
$SesClient\->sendEmail(array(
'Source' \=> 'blahblah@mitchyb.com',
'Destination' \=> array(
'ToAddresses' \=> array(
'blahblah@mitchyb.com'
)
),
'Message' \=> array(
'Subject' \=> array(
'Data' \=> $stackSubject
),
'Body' \=> array(
'Html' \=> array(
'Data' \=> $body
)
)
)
));
}
function updateRecord($elbDns, $cnameDns)
{
global $r53Client;
global $cloudFormationStackName;
// Update DNS Records
try {
$command \= $r53Client\->changeResourceRecordSets(array(
'HostedZoneId' \=> 'Z16PRLGBWGMRUY',
'ChangeBatch' \=> (object) array(
'Changes' \=> (object) array(
array(
'Action' \=> 'UPSERT',
'ResourceRecordSet' \=> array(
'Name' \=> $cnameDns,
'Type' \=> 'CNAME',
'TTL' \=> 60 \* 5,
'ResourceRecords' \=> array(
array(
'Value' \=> $elbDns
)
)
)
)
)
)
));
$msg \= "Route53 record updated to " . $elbDns;
StackNotification($msg, $cnameDns);
}
catch (Exception $e) {
$errorMsg \= "Route53 record update failed with error: $e";
trigger\_error($errorMsg);
StackNotification($errorMsg);
exit;
}
}
;
// Call the record set update function.
updateRecord('offline.mitchyb.com', 'blog.mitchyb.com');