Now that we’ve got everything up and running on the schedule that we want, and being automatically replaced upon failure, we need to ensure our domain alias A record i.e. wiki.easyas.info gets updated with the IP of each new instance as they’re provisioned.
An easy way to do this is to simply execute a script that runs at provisioning time for the instance and updates Route53 with the appropriate IP. A great place to execute this script from is the UserData section of the LaunchConfiguration within the CloudFormation template.
#!/bin/bash DOMAIN=easyas.info HOSTNAME=wiki EC2_PUBLIC=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-hostname` export AWS_ACCESS_KEY_ID=<your-key> export AWS_SECRET_ACCESS_KEY=<your-secret-key> RESULT=`/usr/bin/cli53 rrcreate --replace $DOMAIN "$HOSTNAME 60 A $EC2_PUBLIC"` #e.g. cli53 rrcreate --replace easyas.info "wiki 60 A 22.214.171.124" exit 0
This script was inspired by this answer on Stackoverflow. The script utilises a great little library, cli53 that augments the AWS CLI to provide the needed extra DNS functionality. Alternatively I could use the appropriate AWS CLI command for Route53, which would be cleaner as it wouldn’t require my storing of the AWS Access Keys on the instance. Unfortunately this time around I gave up after 30+ mins trying to work it out with somewhat unhelpful “Invalid request” responses. What I have done is create an AWS user and associated Access Keys for solely updating Route53.
Another way is to avoid the changing IP altogether and simply fork out the few extra $/month for an Elastic IP, they’re free when attached to a running instance. But working within the boundaries of the above configured schedule, it’d cost almost as much to pay for the EIP (0.5c/hr) as it would for the instance (0.6c/hr), based on current US-West-2 / Oregon pricing.