# Transfer a zone to your secondaries

Let your own secondary nameservers copy a hosted zone by AXFR and IXFR, signed with a TSIG key, and tell them about every change with NOTIFY.

Replace _zone-id_ with the id of your hosted zone. With zone transfers on, secondary nameservers
you run (BIND, Knot, PowerDNS) or rent (a provider's secondary DNS) copy the zone from the
platform's nameservers and answer for it too. List them at the registrar next to the platform's
nameservers, and the domain keeps resolving even if every platform nameserver is unreachable.

- **Who may transfer**: the addresses and blocks on the zone's allow-list, and nobody else.
- **TSIG**: every zone with transfers on has a key, `ews-transfer.`_zone_, `hmac-sha256` unless you
  choose another. By default a transfer request not signed with it is refused, and every message of
  the answer is signed.
- **NOTIFY**: when the zone changes, each platform nameserver sends a `NOTIFY` to the zone's
  targets, so the secondaries transfer within seconds rather than at their next refresh (3 hours).
  The targets are the single addresses of the allow-list unless you list others.

## Prerequisites

- Permission for `dns:UpdateZoneTransfer` on the zone to turn transfers on or change them,
  `dns:GetZoneTransfer` to see the settings, `dns:GetZoneTransferKey` to read the key's secret
  (recorded in the audit log), `dns:RotateZoneTransferKey` to replace it and
  `dns:DeleteZoneTransfer` to turn transfers off. In the console, without them, the tab says
  "Turning transfers on needs dns:UpdateZoneTransfer on this zone. Ask an administrator." and greys
  the other buttons with the permission they need.
- In the console, also `dns:ListZones` to open **Hosted zones** and `dns:GetZone` to open the zone.
- DNSSEC off for the zone. A secondary cannot sign answers, so a zone is either signed or
  transferred (see [Sign a zone with DNSSEC](sign-a-zone-with-dnssec.md)).

## Turn transfers on

**Console**
1. Open the zone, then choose the **Zone transfers** tab.
2. For **Secondaries allowed to transfer**, enter the addresses of your secondaries, one per line.
3. Leave **NOTIFY targets** empty to notify those addresses, or list `address` or `address:port`.
4. Keep **Require TSIG** on unless a secondary cannot sign its requests.
5. Choose **Turn on transfers**.

The console shows the key's secret once, under **Transfer key**, and the configuration for BIND
and Knot with the secret filled in.

**CLI**
```sh
dcloud dns zone-transfer set zone-id --allow 198.51.100.53 --allow 2001:db8:53::53
dcloud dns zone-transfer get-key zone-id
```

**API**
```http
PUT /dns/api/zones/zone-id/transfer
{ "allow": ["198.51.100.53", "2001:db8:53::53"], "requireTsig": true }
```

The answer is `201` with the settings and, this once, `secret`: the key's secret in base64.
`GET /dns/api/zones/zone-id/transfer/key` returns it again later.

**Terraform**
```hcl
resource "dcloud_dns_zone_transfer" "main" {
  zone_id = dcloud_dns_zone.main.id
  allow   = ["198.51.100.53", "2001:db8:53::53"]
}
```

The secret never passes through Terraform: read it with the CLI.

The nameservers apply the settings on their next pull, within a minute.

### When it is refused

- `400` "… is not an IP address or a CIDR block (IPv4 /8 or narrower, IPv6 /16 or narrower)".
- `400` "… is not a public address", for a NOTIFY target in a private or reserved range: the
  nameservers send from inside the platform.
- `409` "The zone is signed with DNSSEC. …".

## Configure the secondary

The platform's nameservers are the secondary's primaries: the **Primaries** under **Transfer key**,
or the addresses of the names you delegated to. Transfer from any of them; they serve the same
zone. The console's **Secondary configuration** and `dcloud dns zone-transfer get-key` give the
whole configuration; for BIND it is:

```
key "ews-transfer.example.com" {
    algorithm hmac-sha256;
    secret "<the secret>";
};

zone "example.com" {
    type secondary;
    primaries { 192.0.2.1 key "ews-transfer.example.com"; 192.0.2.2 key "ews-transfer.example.com"; };
    file "secondary/example.com.db";
};
```

Then add the secondary as one more `NS` at the registrar.

## What a secondary gets

- Transfers run over TCP only. A request over UDP is answered empty and truncated, which sends the
  secondary to TCP.
- An `IXFR` from a secondary that has the current serial is answered with the one `SOA`; any other
  `IXFR` is answered with the whole zone, as an `AXFR` would be.
- The apex `NS` set is the platform's nameservers, as every answer says.
- Records as a resolver with no location sees them: a geolocation set its default values, a
  weighted set every variant's values, a failover set and an `ALIAS` the addresses they answer now.
  When those change without a record change (a health check fails, an app moves), the serial goes
  up and the secondaries are told.
- `_acme-challenge` values the platform writes while it issues a certificate are not transferred.

## Rotate the key

A new secret replaces the old one on the nameservers within a minute, and transfers signed with the
old one are refused from then on: put the new secret on the secondaries right away.

**Console**
Under **Transfer key**, choose **New secret**. The console shows it once.

**CLI**
```sh
dcloud dns zone-transfer rotate-key zone-id
```

**API**
```http
POST /dns/api/zones/zone-id/transfer/key/rotate
```

**Terraform:** not available. Terraform does not hold the secret; rotate it with the CLI.

## Turn transfers off

**Console**
Under **Secondaries**, choose **Turn off**.

**CLI**
```sh
dcloud dns zone-transfer delete zone-id
```

**API**
```http
DELETE /dns/api/zones/zone-id/transfer
```

**Terraform**
Destroy the `dcloud_dns_zone_transfer` resource.

The key is deleted, and every transfer is refused from the next pull. The secondaries keep
answering from their copy until it expires (7 days after their last refresh): remove them from the
registrar's `NS` first.

## Related

- [Import or export a zone file](import-or-export-a-zone-file.md), to copy a zone in by `AXFR`
- [Delegate a domain](delegate-a-domain.md)
- `dns.zone-transfer.set` · `dns.zone-transfer.get-key` · `dcloud dns zone-transfer set` ·
  `dcloud_dns_zone_transfer`
