Back to Blog
/Updated /5 min read/By SMTPfast Team

How to Manage Transactional Email Sending Domains With Terraform

Declare the sending domain as a resource in your email provider's Terraform provider, publish the DKIM, SPF, DMARC and MAIL FROM records it returns with your DNS provider's resources, and add a verification resource that waits until the domain can send. With the SMTPfast provider that is three resources and one terraform apply; Amazon SES, Resend, Mailgun and SendGrid have providers too.

terraforminfrastructure-as-codedevopsapidomains
Share:𝕏in

The short answer: declare the sending domain as a resource in your email provider's Terraform provider, pass the DNS records it returns to your DNS provider's record resource with for_each, and add a resource that waits for verification before anything sends. With the SMTPfast provider that is three resources and one terraform apply.

Doing it by hand is fine the first time: add a domain, copy the DKIM, SPF and DMARC records into your DNS provider, wait, create an API key, paste it into a secret store. Then you do it again for staging, a teammate does it slightly differently for the EU environment, and six months later nobody knows which webhook points where. In Terraform the setup is a diff that someone reviews.

Which email providers have a Terraform provider?

Checked on the Terraform Registry on 21 November 2026:

Email service Terraform provider Published by Sending domain resource
SMTPfast smtpfast/smtpfast SMTPfast smtpfast_domain, plus smtpfast_domain_verification to wait
Amazon SES hashicorp/aws HashiCorp aws_sesv2_email_identity
Resend Softwarehuset-com/resend Community author resend_domain, plus resend_domain_verification
Mailgun wgebis/mailgun, hackthebox/mailgun Community authors mailgun_domain
SendGrid kenzo0107/sendgrid, anna-money/sendgrid Community authors sendgrid_sender_authentication or sendgrid_domain_authentication
Brevo bbieniek/brevo Community author brevo_domain

A community provider can be good, but it follows the vendor's API on its own schedule, and nobody at the vendor answers its issues. Check its last release and open issues before you put production DNS behind it.

What the SMTPfast provider manages

Version 0.7.0 has these resources:

  • smtpfast_domain: a sending domain. It returns a records map with fixed keys (dkim_1, dkim_2, dkim_3, spf, dmarc, mail_from_mx, mail_from_spf, and receiving_mx when inbound is on). receiving_enabled turns on inbound email.
  • smtpfast_domain_verification: waits until the domain can send.
  • smtpfast_api_key: a scoped API key.
  • smtpfast_webhook: an endpoint and its events, as signed JSON or as readable Discord alerts.
  • smtpfast_link_domain: a custom domain for tracking and unsubscribe links.
  • smtpfast_template, smtpfast_segment, smtpfast_contact_property and smtpfast_signup_form.
  • smtpfast_inbox and smtpfast_inbox_label for Inboxes.
  • smtpfast_team_member and smtpfast_team_invite.

Data sources: smtpfast_domain and smtpfast_segment. The source is on GitHub under MPL-2.0.

Set up the provider

The key can come from a variable or the SMTPFAST_API_KEY environment variable, so nothing secret lives in your config:

terraform {
  required_providers {
    smtpfast = {
      source = "smtpfast/smtpfast"
    }
    cloudflare = {
      source  = "cloudflare/cloudflare"
      version = "~> 5.0"
    }
  }
}

provider "smtpfast" {}

For domains, the provider's key needs the domain:read and domain:write scopes and must be created by a team owner or admin.

Register a domain and publish its DNS in one apply

resource "smtpfast_domain" "mail" {
  domain = "mail.example.com"

  # Deleting the domain stops all sending from it, and adding it again
  # gives new DKIM records.
  lifecycle {
    prevent_destroy = true
  }
}

resource "cloudflare_dns_record" "smtpfast" {
  for_each = smtpfast_domain.mail.records

  zone_id  = var.cloudflare_zone_id
  type     = each.value.type
  name     = each.value.name
  content  = each.value.value
  priority = each.value.priority # MX records only, null otherwise
  ttl      = 1                   # automatic
  proxied  = false               # a proxied DKIM CNAME does not resolve
}

# Wait until the domain can send.
resource "smtpfast_domain_verification" "mail" {
  domain_id  = smtpfast_domain.mail.id
  depends_on = [cloudflare_dns_record.smtpfast]
}

The keys of records are known before the domain exists, so the for_each plans on the first run, and a changed value updates that DNS record in place instead of replacing it. smtpfast_domain_verification asks SMTPfast to check the domain and keeps checking until it is verified or 30 minutes pass. If it times out, the error lists each record SMTPfast does not see yet and what it found instead, which is faster than comparing two browser tabs.

Route 53 works the same way with aws_route53_record; put the MX priority in the record value. If the domain already has an SPF record for another service, do not add a second one: merge SMTPfast into the existing record with spf_include. Check the result with the SPF checker, or see how to check SPF, DKIM and DMARC records.

API keys as code

Scope each key to what its workload needs. The secret is only returned at create time, so send it straight to where it belongs:

resource "smtpfast_api_key" "app" {
  name   = "app-send-only"
  scopes = ["email:send", "email:read"]
}

output "app_api_key" {
  value     = smtpfast_api_key.app.key
  sensitive = true
}

A key can only create keys with scopes it has itself, and never apikey:manage, so the provider's key needs apikey:manage plus every scope it hands out. See keys that manage keys.

The key is written to state. Treat state like a secret store: a remote backend with encryption at rest, never a file in the repository.

Webhooks as code

Keep the event list in version control, where a reviewer sees when someone adds email.complained:

resource "smtpfast_webhook" "events" {
  url = "https://example.com/webhooks/smtpfast"
  events = [
    "email.delivered",
    "email.bounced",
    "email.complained",
  ]
}

output "webhook_signing_secret" {
  value     = smtpfast_webhook.events.signing_secret
  sensitive = true
}

Read an existing domain

If the domain was created before you adopted Terraform, the data source reads its records by name or id:

data "smtpfast_domain" "existing" {
  domain = "mail.example.com"
}

output "spf_record_name" {
  value = data.smtpfast_domain.existing.records["spf"].name
}

Why do it this way

  • Staging and production match. One module builds both, so a domain that verifies in staging verifies in production.
  • Changes are reviewed. Adding a webhook event or rotating a key is a diff, not a dashboard click nobody logged.
  • Recovery is boring. If an environment is lost, terraform apply rebuilds the domains, keys and webhooks.

The full reference is on the Terraform Registry, including a complete Cloudflare guide. If you do not have an account yet, create one free and send your first email in a few minutes.

Frequently asked questions

How do I manage transactional email sending domains with Terraform?

Use your email provider's Terraform provider to create the domain, then pass the DNS records it returns to your DNS provider's record resource (cloudflare_dns_record, aws_route53_record) with for_each. With SMTPfast, smtpfast_domain returns a records map (dkim_1 to dkim_3, spf, dmarc, mail_from_mx, mail_from_spf), and smtpfast_domain_verification waits until the domain can send.

Is there an official Terraform provider for Resend, Mailgun or SendGrid?

On the Terraform Registry (checked 21 November 2026), the providers for Resend (Softwarehuset-com/resend), Mailgun (wgebis/mailgun, hackthebox/mailgun) and SendGrid (kenzo0107/sendgrid, anna-money/sendgrid) are published by community authors, not by the email companies. Amazon SES is covered by the official hashicorp/aws provider. The SMTPfast provider (smtpfast/smtpfast) is published by SMTPfast.

Can Terraform wait until a sending domain is verified?

Yes, with a resource that polls. smtpfast_domain_verification asks SMTPfast to check the domain and checks again until it is verified or the create timeout ends (30 minutes by default). On timeout, the error lists each DNS record SMTPfast does not see yet and what it found instead. The community Resend provider has a similar resend_domain_verification resource.

Is it safe to keep API keys in Terraform state?

Only if the state is protected like a secret store: a remote backend with encryption at rest and limited access, never a state file in the repository. The smtpfast_api_key secret is only returned at create time and is marked sensitive, but it is still written to state.

Ready to get started?

Start sending transactional email today. Free to start, no credit card required.

Start free