AWS Ruby SDK - Adding a Friendly DNS name to Route 53

- 1 min

If you are using Auto Scaling with AWS, you know it can be difficult to keep track of everything that’s going on. To make matter’s worse if you are using the default hostnames that come with your EC2 Instance all of a sudden your Monit or Nagios Alert’s can become even difficult to understand.

Most likely if you are using Auto Scaling, you already have some sort of bootstrap process and during that you might want to add a friendly DNS record to Route 53.

Below is a snippet of the code I use to do this using the AWS SDK for Ruby.

http://docs.aws.amazon.com/AWSRubySDK/latest/frames.html

require 'rubygems'
require 'aws-sdk'

r53 = AWS::Route53.new

# unique id - could be the instance id
instance_name  = 'webserver-autoscaling-{unique-id}.example.org'
public_ip      = '123.123.123.123'
hosted_zone_id = 'ABC123'


r53_options = {
    :comment => "auto scaling awesomness - *optional*",
    :changes => [{
        :action => "CREATE",
            :resource_record_set => {
                :type => "A",
                :resource_records => [{
                    :value => public_ip
                }],
                :name => instance_name,
                :ttl => 300
             }
    }]
}



# make sure check the response for errors
r53.client.change_resource_record_sets(:hosted_zone_id => hosted_zone_id, :change_batch => r53_options)

I assign an IAM Role to the EC2 Instance’s upon creation. This make’s it easy to create very restricted policies, essentially locking down your instance to a small set of API calls and as bonus you no longer have to hardcode API Key’s in your scripts.

http://docs.aws.amazon.com/IAM/latest/UserGuide/WorkingWithRoles.html

Drew


comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora