Managing Records

Managing DNS records for the Cloud DNS API involves sending change requests to the API. These changes consist of additions and deletions to your resource record sets collection. You can easily send the desired changes to the API using the import, export, and transaction commands, as described below.

Before you begin

You must have already created a managed zone and completed the prerequisites for that task.

For all the gcloud command-line examples below, you can also specify the --project parameter for a command to operate against a different project for that invocation.

Importing and exporting record-sets

You can use import and export to copy record-sets into and out of a managed zone. The formats you can import from and export to are either BIND zone file format, or YAML records format.

To import record-sets, you use the dns record-sets import command. The --zone-file-format flag tells import to expect a BIND zone formatted file. If you omit this flag, import expects a YAML formatted records file:

gcloud dns record-sets import -z=examplezonename \
   --zone-file-format path-to-example-zone-file

To export record-sets, you use the dns record-sets export command. Use the --zone-file-format flag to tell export to export the record-sets into a BIND zone formatted file. If you omit this flag, export exports the record-sets into a YAML formatted records file:

gcloud dns record-sets export example.zone -z=examplezonename \
   --zone-file-format
   

Adding or removing records in a record set using transactions

You add or remove DNS records in a record set by creating and executing a transaction that specifies the operations you want to perform. A transaction is a group of one or more record changes that should be propagated together. The entire transaction either succeeds or fails, so your data is never left in an intermediate state.

To add or remove a DNS record in a record set by using a transaction, follow these steps:

  1. Start a transaction by running the start command:

    gcloud dns record-sets transaction start -z=examplezonename

    Whenever you run the start command, Cloud DNS creates a local file in YAML format called transaction.yaml.

  2. Run transaction commands to add or remove DNS records.

    The add command appends an A record addition to the transaction. For example:

    gcloud dns record-sets transaction add -z=examplezonename \
       --name="www.example.com." \
       --type=A \
       --ttl=300 "7.5.7.8"

    The remove command appends a record-set removal to the transaction. For example:

    gcloud dns record-sets transaction remove -z=examplezonename \
        --name="mail.example.com." \
        --type=A \
       --ttl=300 "7.5.9.99"

    To replace an existing record, issue a remove command followed by an add command. For example:

    gcloud dns record-sets transaction add -z=examplezonename \
       --name="mail.example.com." \
       --type=A \
       --ttl=300 "7.5.9.199"

    As you run transaction commands, they are added to the transaction.yaml file.

  3. When you're finished running the transaction commands, execute the transaction by running the execute command. For example:

    gcloud dns record-sets transaction execute -z=examplezonename

    After you execute or abort a transaction, the transaction.yaml file is deleted.

Displaying the current record set

To display the current DNS records for your zone:

Command line

gcloud dns record-sets list --zone="myzonename"
The command will output the JSON response for the resource record set for the first 100 records. You can specify these additional parameters:
  • limit: Maximum number of record-sets to list.
  • name: Only list record-sets with this exact domain name.
  • type: Only list records of this type. If present, the --name parameter must also be present.

Python

def list_resource_records(project_id, zone_name):
    client = dns.Client(project=project_id)
    zone = client.zone(zone_name)

    records = zone.list_resource_record_sets()

    return [(record.name, record.record_type, record.ttl, record.rrdatas)
            for record in records]

Next steps

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud DNS Documentation