Support Library
Managing DNS with Terraform
Terraform (and its open-source fork OpenTofu) can manage DNS records alongside the infrastructure they point to, so a record change is reviewed and recorded like any other code change. It also makes it easy to delete a whole zone by accident. This guide covers a safe way to work.
The short answer
Describe each zone and record as a resource, import the records that already exist before Terraform manages them, and change DNS only through a reviewed terraform plan that you then apply exactly as reviewed. Keep state in a locked remote backend, give the credentials access to the DNS zones and nothing else, protect zones from deletion, and lower TTLs in one change before you change the values in the next. The examples below use AWS Route 53, but the same pattern applies to any provider with a Terraform provider, such as Cloudflare, Azure DNS, Google Cloud DNS or NS1.
A small example
resource "aws_route53_zone" "main" {
name = "example.com"
lifecycle {
prevent_destroy = true
}
}
resource "aws_route53_record" "www" {
zone_id = aws_route53_zone.main.zone_id
name = "www.example.com"
type = "A"
ttl = 300
records = ["192.0.2.10"]
}
resource "aws_route53_record" "mx" {
zone_id = aws_route53_zone.main.zone_id
name = "example.com"
type = "MX"
ttl = 3600
records = ["10 mail.example.com."]
}- One resource per record set (name and type), with all its values in records. Two resources for the same name and type fight each other.
- prevent_destroy on the zone. Deleting and recreating a hosted zone deletes every record in it, and on most providers the new zone gets new nameservers, so the delegation at your registrar breaks too.
- Values are written the way the provider stores them. Route 53 wants MX priority and target in one string; other providers have separate fields. Read the provider's documentation for each record type.
Bringing existing records under Terraform
Never apply a new configuration over a zone that already has records without importing them first. Terraform 1.5 and later (and OpenTofu) can import with an import block and write the matching configuration for you. Import the zone itself first: if only the records are imported, Terraform plans a new, empty hosted zone and then moves the records into it, deleting them from the live, delegated zone.
import {
to = aws_route53_zone.main
id = "Z0123456789ABCDEFGHIJ"
}
import {
to = aws_route53_record.www
id = "Z0123456789ABCDEFGHIJ_www.example.com_A"
}| Step | Command |
|---|---|
| Generate configuration for every import block | terraform plan -generate-config-out=generated.tf |
| Check the import changes nothing | terraform plan should show imports and no changes |
| Record the imports in state | terraform apply |
Import IDs differ between providers; Route 53 uses the zone ID, name and type joined with underscores. Export the zone first (see DNS drift) so you have a list of every record to import and a copy to roll back to.
Reviewing a plan
| Command | Why |
|---|---|
| terraform plan -out=dns.tfplan | Saves the exact plan that will be applied |
| terraform show dns.tfplan | Lets a reviewer read it |
| terraform apply dns.tfplan | Applies what was reviewed and nothing else |
Read every line, and stop at these:
- must be replaced on a record means delete then create. Changing a record's name or type does this, and for a moment the name does not exist. Resolvers that ask in that moment cache a negative answer.
- Any destroy you did not expect, especially of a zone.
- Changes to NS or DS records, which affect the delegation and DNSSEC and are slow to undo.
- TTL and value changes in the same plan. Lower the TTL in one apply, wait for the old TTL, then change the value, as described in planning a DNS change.
State and credentials
- Use a remote backend with locking (for example S3 with a lock, Terraform Cloud, or your CI's backend) so two applies cannot run at once. Restrict who can read state: it holds every record and provider ID.
- Scope credentials to DNS. On Route 53 that means allowing record changes only on the zones Terraform manages (route53:ChangeResourceRecordSets, route53:ListResourceRecordSets, route53:GetHostedZone and route53:ListTagsForResource on those zones (the AWS provider reads zone tags on every plan and import, even when you set none), plus route53:GetChange on arn:aws:route53:::change/*, which Terraform uses to wait for each change to sync and which does not attach to a zone). On Cloudflare, an API token limited to DNS edit on named zones. Other providers have similar scoped tokens or roles.
- Keep secrets out of code. Use environment variables locally and short-lived credentials (OIDC) in CI. See safe DNS changes in CI/CD.
Drift and rollback
| Command | What it does |
|---|---|
| terraform plan -refresh-only | Shows records that were changed outside Terraform, without changing anything |
Decide whether to adopt each change into code or revert it; do not apply over it blindly. DNS drift covers that review.
To roll back, revert the commit and run a new plan and apply. That works for record values, and with a short TTL users follow within minutes. It does not undo a deleted zone (new nameservers) or a DNSSEC key change, which is why those deserve prevent_destroy and a separate, careful change.
Limits to know
- Terraform only manages records it has resources for. Records created by hand or by other tools are left alone, so a clean plan does not mean the zone matches your code.
- Provider-specific features, such as proxying, alias records, traffic policies or health checks, have provider-specific resources and do not move between providers.
- Large zones make slow plans, because every record is refreshed. Splitting zones into separate state files helps.
- Automation that writes its own records (ACME clients, Kubernetes ExternalDNS) should own separate names, so Terraform does not delete them.
How DNS Panopticon helps
DNS Panopticon does not run Terraform or read your code. After an apply, Security Posture and Domain Visualizer show the domain as the public sees it, and the DNS Propagation Checker confirms a changed record has reached the authoritative servers and public resolvers.
FAQ
Should every record be in Terraform?
Either all records in a zone, or a clearly separate set of names. A zone half in code and half edited by hand drifts quickly and makes plans hard to trust.
What happens if I delete a zone resource?
Terraform deletes the zone and all its records. If it is recreated, most providers assign new nameservers, so the delegation at your registrar must be updated too. Use prevent_destroy on zones.
Is OpenTofu different for DNS?
Not in practice. OpenTofu uses the same providers, configuration language, import blocks and plan workflow for DNS records.
Can I manage the same zone with two providers?
Yes, with a resource set for each provider fed from the same variables, but record types and features must be supported by both. DNSControl and octoDNS are built for this case.