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
transaction commands, as
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
Importing and exporting record-sets
You can use
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.
--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.
--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:
Start a transaction by running the
gcloud dns record-sets transaction start -z=examplezonename
Whenever you run the
startcommand, Cloud DNS creates a local file in YAML format called
Run transaction commands to add or remove DNS records.
addcommand 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 "220.127.116.11"
removecommand 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 "18.104.22.168"
To replace an existing record, issue a
removecommand followed by an
addcommand. For example:
gcloud dns record-sets transaction add -z=examplezonename \ --name="mail.example.com." \ --type=A \ --ttl=300 "22.214.171.124"
As you run transaction commands, they are added to the
When you're finished running the transaction commands, execute the transaction by running the
executecommand. For example:
gcloud dns record-sets transaction execute -z=examplezonename
After you execute or abort a transaction, the
transaction.yamlfile is deleted.
Displaying the current record set
To display the current DNS records for your zone:
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
--nameparameter must also be present.
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]