[AWS] Amazon Route 53: Creating many records using command line

I have a domain zone registered in Amazon Route 53. For instance, "mydomain.com".

The initial task is to create 100 sub-domains in this zone in form "<number>.mydomain.com". I.e. 0.mydomain.com, 1.mydomain.com, .... 99.mydomain.com.

All of them must refer to one machine. For example, 8.8.8.8

In this post I will describe precise steps to accomplish the task using Linux command line and Route 53 API.


1. Download dnscurl.pl script here: http://aws.amazon.com/developertools/9706686376855511

Also read and check requirements carefully. I had all requirements satisfied so I didn't have any problems running the script.

2. Go to Security page on AWS Management Console https://aws-portal.amazon.com/gp/aws/securityCredentials and notice "Access Key ID" and "Secret Access Key" in the middle of the page.

3. Create file $HOME/.aws-secrets with the following content:

%awsSecretAccessKeys = (
    "my-aws-account" => {
        id => "<Access Key Id>",
        key => "<Secret Access Key>",
    },
);

Obviously don't forget to change id/key to the values found on Security page.

4. Create file create_ntfy_zones.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<ChangeResourceRecordSetsRequest xmlns="https://route53.amazonaws.com/doc/2012-02-29/">
   <ChangeBatch>
      <Comment>
      </Comment>
      <Changes>
         <Change>
            <Action>CREATE</Action>
            <ResourceRecordSet>
               <Name>__ZONE__.mydomain.com.</Name>
               <Type>A</Type>
               <TTL>300</TTL>
               <ResourceRecords>
                  <ResourceRecord>
                     <Value>8.8.8.8</Value>
                  </ResourceRecord>
               </ResourceRecords>
            </ResourceRecordSet>
         </Change>
      </Changes>
   </ChangeBatch>
</ChangeResourceRecordSetsRequest>


Put your appropriate values instead of mydomain.com and 8.8.8.8.

5. Go to Route 53 page in AWS Management Console: https://console.aws.amazon.com/route53/home and find in the column "Hosted Zone ID" the id of zone in which you are going to create records.

It looks like this: ZXHLYZ5FBVHI

6. Run the following command:

for((I=10;I<99;I++)); do sed -e "s#__ZONE__#$I#" < create_ntfy_zones.xml > request.xml; ./src/dnscurl.pl --keyname my-aws-account -- -X POST --upload-file request.xml https://route53.amazonaws.com/2012-02-29/hostedzone/<Hosted Zone ID>/rrset  ; done

Enjoy and wait.

Conclusion:
The similar approach could be used to modify/delete multiple records.


References:

http://docs.amazonwebservices.com/Route53/latest/APIReference/API_ChangeResourceRecordSets.html
http://aws.amazon.com/developertools/9706686376855511
http://dmz.us/2010/12/amazon-route-53-dns/

No comments: