porkbunwith Terraform

Porkbun domains and DNS, as code.

Manage Porkbun DNS records, DNSSEC, nameservers, glue records and URL forwarding from Terraform or OpenTofu, and register domains with a price ceiling. Porkbun doesn't publish its own Terraform provider: the providers below are community-built on Porkbun's public API, which Porkbun builds and supports. DNSControl, octoDNS, external-dns and cert-manager work with Porkbun too.

The providers

Both are on the Terraform Registry and the OpenTofu registry, and both are maintained by independent developers, not by Porkbun. File provider bugs on their GitHub repositories.

jianyuan/porkbun

The most complete, and actively developed.

Manages: DNS records, DNSSEC, nameservers, glue records, URL forwarding, auto-renew, and domain registration (with a price ceiling).

Credentials: PORKBUN_API_KEY and PORKBUN_SECRET_KEY

Source on GitHub

marcfrederick/porkbun

A stable 1.x release covering DNS.

Manages: DNS records, DNSSEC, nameservers, URL forwarding.

Credentials: PORKBUN_API_KEY and PORKBUN_SECRET_API_KEY

Source on GitHub

Mind the secret key's variable name: it differs between the two, and copying one provider's setup into the other is the usual cause of an authentication error. They also name the MX and SRV priority differently (priority in jianyuan/porkbun, prio in marcfrederick/porkbun).

Set it up

  1. Create an API key at porkbun.com/account/api. For a pipeline that only manages DNS, give the key just the dns permission (plus read) and limit it to the domains and source IPs it needs.
  2. Turn on API access for those domains. It is opt-in per domain in Domain Management (one at a time or in bulk) unless the account has it on for every domain.
  3. Export the keys: export PORKBUN_API_KEY=pk1_... and the secret under your provider's variable name.
  4. Write the config, then terraform plan and terraform apply (or tofu):
terraform {
  required_providers {
    porkbun = {
      source = "jianyuan/porkbun"
    }
  }
}

# Reads PORKBUN_API_KEY and PORKBUN_SECRET_KEY from the environment.
provider "porkbun" {}

resource "porkbun_dns_record" "apex" {
  domain  = "example.com"
  type    = "A"
  content = "203.0.113.10"
}

resource "porkbun_dns_record" "www" {
  domain    = "example.com"
  subdomain = "www"
  type      = "CNAME"
  content   = "example.com"
}

resource "porkbun_dns_record" "mail" {
  domain   = "example.com"
  type     = "MX"
  content  = "mx1.example.net"
  priority = 10
}

Already have records? Bring them under management with an import block instead of recreating them. jianyuan/porkbun's record IDs are <record id>_<domain>_<type>; the record ID is in the API's /dns/retrieve response.

import {
  to = porkbun_dns_record.www
  id = "123456789_example.com_CNAME"
}

Test it without touching anything real

A Porkbun sandbox key, which starts pk1_sb_, uses the same API address as a live key, so any provider works with one unchanged. Every DNS, nameserver and registration call then runs against an isolated environment with fake credit: no real records change and nothing is charged. Get one at porkbun.com/account/api, or instantly with no account by sending {"sandbox": true} to /apikey/request (details).

With live keys, the account's guardrails still apply to Terraform: the monthly API spend limit, low-balance alerts, and per-key domain, IP and permission limits.

Registrations are permanent. terraform destroy on a porkbun_domain only drops it from state, because no API can undo a registration; the domain stays yours until it expires. Use max_cost and prevent_destroy.

Other DNS-as-code and certificate tools

Not on Terraform? These manage Porkbun DNS directly.

DNSControlPorkbun is a built-in provider. Zones as JavaScript, with a preview before every push.
octoDNSZones as YAML, synced to Porkbun through the octodns-porkbun plugin.
external-dnsKubernetes Services and Ingresses publish their own records, through a community webhook provider.
cert-managerLet's Encrypt DNS-01 certificates in Kubernetes, through a community webhook.
lego and Traefiklego has Porkbun built in for DNS-01, and Traefik uses lego, so wildcard certificates work out of the box.
CaddyThe caddy-dns/porkbun module lets Caddy issue wildcard certificates over DNS-01.
CertbotThe certbot-dns-porkbun plugin does DNS-01 validation for Certbot.

Prefer an official provider for the zone itself? Keep the registration at Porkbun and point the nameservers at any DNS host, or move the zone into your own Cloudflare account with Cloudflare connect and manage it with Cloudflare's provider.

Questions

Does Porkbun work with Terraform?

Yes. Community Terraform providers built on Porkbun's public API manage DNS records, DNSSEC, nameservers, glue records and URL forwarding, and one can register domains. They work the same in OpenTofu, and DNSControl, octoDNS, external-dns and cert-manager can manage Porkbun DNS too.

Is there an official Porkbun Terraform provider?

No. Porkbun does not publish its own provider. The providers on the Terraform Registry, jianyuan/porkbun and marcfrederick/porkbun, are written by independent developers on top of Porkbun's public JSON API, which Porkbun does build and support. Report provider bugs to the provider's GitHub repository; report API problems to Porkbun.

Which Porkbun Terraform provider should I use?

jianyuan/porkbun is the most complete: DNS records, DNSSEC, nameservers, glue, URL forwarding, auto-renew and domain registration. marcfrederick/porkbun is a stable 1.x release if you only need DNS, nameservers and URL forwarding. Both are on the Terraform and OpenTofu registries.

What environment variables do the providers use?

Both read the API key from PORKBUN_API_KEY. For the secret key, jianyuan/porkbun reads PORKBUN_SECRET_KEY and marcfrederick/porkbun reads PORKBUN_SECRET_API_KEY. Copying one provider's setup into the other is the usual cause of an authentication error.

Can Terraform register a domain at Porkbun?

Yes, with jianyuan/porkbun's porkbun_domain resource. It quotes the live price first, and a max_cost setting refuses the purchase if the quote is higher. Registrations are paid from prepaid account credit and count against the account's monthly API spend limit.

Does terraform destroy delete my domain?

No. A registration cannot be undone through the API, so destroying a porkbun_domain resource only removes it from Terraform state; the domain stays registered until it expires. Destroying a DNS record resource does delete the record.

Can I test a Terraform config without touching real DNS or spending money?

Yes. Porkbun sandbox keys (they start pk1_sb_) use the same API address as live keys, so a provider pointed at a sandbox key runs DNS, nameserver and registration operations against an isolated environment with fake credit. You can get one instantly, with no account, from the API.

Can I keep my domains at Porkbun but manage DNS with another provider?

Yes. Set the domain's nameservers to any DNS host and manage the zone with that host's own Terraform provider, or use Porkbun's Cloudflare connect to move the zone into your own Cloudflare account and manage it with Cloudflare's provider. The registration stays at Porkbun.