Skip to main content

route53

aws command

aws route53 list-hosted-zones
aws route53 get-hosted-zone --id /hostedzone/xxxxxxxxxxxxxxxxxxxxx

aws route53 list-resource-record-sets   --hosted-zone-id /hostedzone/xxxxxxxxxxxxxxxxxxxxx --query "ResourceRecordSets[?Name == 'example.example.net.']"
aws route53 test-dns-answer --hosted-zone-id /hostedzone/xxxxxxxxxxxxxxxxxxxxx --record-name "example.example.net" --record-type "A"

aws route53 change-resource-record-sets  --hosted-zone-id /hostedzone/xxxxxxxxxxxxxxxxxxxxx --change-batch file://create.json
aws route53 get-change --id /change/xxxxxxxxxxxxxxxxxxxx
aws route53 change-resource-record-sets  --hosted-zone-id /hostedzone/xxxxxxxxxxxxxxxxxxxxx --change-batch file://delete.json
aws route53 get-change --id /change/xxxxxxxxxxxxxxxxxxxx

create.json

{
  "Changes": [
    {
      "Action": "CREATE",
      "ResourceRecordSet": {
        "Name": "example.example.net",
        "Type": "A",
        "TTL": 3600,
        "ResourceRecords": [
          {
            "Value": "xxx.xxx.xxx.xxx"
          }
        ]
      }
    }
  ]
}

delete.json

{
  "Changes": [
    {
      "Action": "DELETE",
      "ResourceRecordSet": {
        "Name": "example.example.net",
        "Type": "A",
        "TTL": 3600,
        "ResourceRecords": [
          {
            "Value": "xxx.xxx.xxx.xxx"
          }
        ]
      }
    }
  ]
}

python sample script

#! /usr/bin/python2
import boto3
import time

client = boto3.client('route53')
zones = client.list_hosted_zones()

for zone in zones['HostedZones']:
    id =zone['Id']
    name = zone['Name']
    #print(client.get_hosted_zone(Id=id).get('HostedZone'))
    for set in client.list_resource_record_sets(HostedZoneId=id).get('ResourceRecordSets'):
        print(set)
    #action = 'CREATE'
    action = 'DELETE'
    address = 'xxx.xxx.xxx.xxx'

    batch = {
        'Changes': [
            {
                'Action': action,
                'ResourceRecordSet': {
                    'Name': 'example.%s' % name,
                    'Type': 'A',
                    'TTL': 3600,
                    'ResourceRecords': [
                        {
                            "Value": address
                        },
                    ],
                }
            },
        ]
    }
    response = client.change_resource_record_sets(
        HostedZoneId=id,
        ChangeBatch=batch
    )
    print(response)
    responseid = response['ChangeInfo']['Id']
    status = response['ChangeInfo']['Status']
    while ( status != 'INSYNC'):
        response = client.get_change(Id=responseid)
        status = response['ChangeInfo']['Status']
        time.sleep(30)
    else:
        print(response)

cloud formation sample stack

aws cloudformation validate-template --template-body file://route53.yml
aws cloudformation create-stack --template-body file://route53.yml --parameters file://parameters.json --stack-name route53test
aws cloudformation describe-stack-events --stack-name route53test
aws cloudformation list-stack-resources --stack-name route53test
aws cloudformation delete-stack --stack-name route53test

route53.yml

Parameters:
  HostedZoneId:
    Type: String
  Domain:
    Type: String
  Address:
    Type: String
Resources:
  myDNSRecord:
    Type: AWS::Route53::RecordSet
    Properties:
      HostedZoneId : 
         Ref: HostedZoneId
      Name:  
        Fn::Join: 
            - '.'
            - - 'example'
              - !Ref Domain
      ResourceRecords:
      - Ref: Address
      TTL: '3600'
      Type: A

parameters.json

[
  {
      "ParameterKey": "Address",
      "ParameterValue": "xxx.xxx.xxx.xxx"
  },
  {
      "ParameterKey": "HostedZoneId",
      "ParameterValue": "xxxxxxxxxxxxxxxxxxxxx"
  },
  {
      "ParameterKey": "Domain",
      "ParameterValue": "example.net"
  }
]