Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a route53 record set using the aws sdk for ruby?

EC2 gives instances a new IP address when they're stopped then restarted, so I need to be able to automatically manage a route53 record set so that I can access things consistently. Sadly the documentation for the route53 portion of the sdk is not nearly as robust as it is for ec2 (understandably) and so I'm a bit stuck. From what I've seen so far, it seems like change_resource_record_sets (link) is the way to go, but I'm confused as to what needs go into :chages since it mentions a Change object but fails to provide a link to a description of said object.

Here's what my code currently looks like for a creation:

r53.client.change_resource_record_sets(:hosted_zone_id => 'MY_ID', :change_batch => {
    :changes => 'I DONT KNOW WHAT GOES HERE',
    :action => 'CREATE',
    :resource_record_set => {
        :name => @instance.instance_name,
        :type => 'CNAME',
        :ttl => 330,
        :value => @instance.ip_address
}})

EDIT: Okay, since I haven't had any help either here or on the official forums I've been messing around with it myself. So it turns out that the documentation is just plain awful. All of the values are stored in a Change object, and not given there. So it actually looks more like this:

some_change = AWS::Route53::CreateRequest.new(@instance.instance_name,
                                             'CNAME',
                                             :ttl => 330,
                                             :resource_records => [
                                                 {:value => @instance.ip_address}
                                             ])

r53.client.change_resource_record_sets(:hosted_zone_id => 'MY_ZONE', :change_batch => {
    :changes => [some_change],
})
like image 953
Slippery John Avatar asked Jun 26 '13 14:06

Slippery John


People also ask

What is record set in AWS Route 53?

Amazon Route 53 offers a special type of record called an 'Alias' record that lets you map your zone apex (example.com) DNS name to the DNS name for your ELB load balancer (such as my-loadbalancer-1234567890.us-west-2.elb.amazonaws.com).

What is AWS SDK Ruby?

The AWS SDK for Ruby helps you to get started building applications using Amazon Web Services infrastructure services, including Amazon S3, Amazon EC2, Amazon DynamoDB, and more.


1 Answers

The documentation is pretty poor, though it does contain a few examples. I initially had the impression that it was necessary to create requests (and use the client object) per your solution, but there are alternatives.

An example of creating a record can be found in the ResourceRecordSetCollection reference, but it's only a little more concise than your answer:

rrsets = AWS::Route53::HostedZone.new(hosted_zone_id).rrsets
rrset = rrsets.create('foo.example.com.', 'A', :ttl => 300, :resource_records => [{:value => '127.0.0.1'}])

I wanted to update an existing record, and didn't have the hosted_zone_id to hand. It took far too long to figure out how best to do this, so I'm offering the following example in the hope it saves someone else some time:

r53    = AWS::Route53.new
domain = "example.com."
fqdn   = "host." + domain
zone   = r53.hosted_zones.select { |z| z.name == domain }.first
rrset  = zone.rrsets[fqdn, 'A']
rrset.resource_records = [ { :value => "1.2.3.4" } ]
rrset.update

Note that that assumes you only have a single zone with that name in Route53.

like image 189
zts Avatar answered Sep 22 '22 18:09

zts