Monday, June 01, 2015

Route53 + RaspberryPi + Cron + PHP =  lazy admin.

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. 

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]";
        'Source' => '',
        'Destination' => array(
            'ToAddresses' => array(
        '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(
                        'Action' => 'UPSERT',
                        'ResourceRecordSet' => array(
                            'Name' => $cnameDns,
                            'Type' => 'CNAME',
                            'TTL' => 60 * 5,
                            'ResourceRecords' => array(
                                    'Value' => $elbDns
        $msg = "Route53 record updated to " . $elbDns;
        StackNotification($msg, $cnameDns);
    catch (Exception $e) {
        $errorMsg = "Route53 record update failed with error: $e";
// Call the record set update function.
updateRecord('', '');

No comments:

A little about Me

My photo
My name is Mitch Beaumont and I've been a technology professional since 1999. I began my career working as a desk-side support engineer for a medical devices company in a small town in the middle of England (Ashby De La Zouch). I then joined IBM Global Services where I began specialising in customer projects which were based on and around Citrix technologies. Following a couple of very enjoyable years with IBM I relocated to London to work as a system operations engineer for a large law firm where I responsible for the day to day operations and development of the firms global Citrix infrastructure. In 2006 I was offered a position in Sydney, Australia. Since then I've had the privilege of working for and with a number of companies in various technology roles including as a Solutions Architect and Technical team leader.