Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(domain): a little more ergonomic DNS record configuration #39

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

magurotuna
Copy link
Member

This commit adds dns_record_a, dns_record_aaaa, and dns_record_cname fields to the output of deno_domain resource so that the following steps where DNS records are added can reference them by the record types like cname instead of indexes.

Currently a typical configuration looks like this, where DNS records registration uses indexes such as [0] to get values to be set. This works, but definitely is not best because the length of dns_records is unclear from the type definition, plus we cannot tell which record corresponds to which record type.

# Add a new domain to your organization.
resource "deno_domain" "example" {
  domain = "foo.example.com"
}

# Add DNS records to the nameserver.
resource "cloudflare_record" "my_record_0" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[0].name
  type    = upper(deno_domain.example.dns_records[0].type)
  value   = deno_domain.example.dns_records[0].content
  proxied = false
  ttl     = 120
}

resource "cloudflare_record" "my_record_1" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[1].name
  type    = upper(deno_domain.example.dns_records[1].type)
  value   = deno_domain.example.dns_records[1].content
  proxied = false
  ttl     = 120
}

resource "cloudflare_record" "my_record_2" {
  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[2].name
  type    = upper(deno_domain.example.dns_records[2].type)
  value   = deno_domain.example.dns_records[2].content
  proxied = false
  ttl     = 120
}

With this commit, these issues are resolved and now we can write like below. One can also choose not to use for_each if they wish.

# Add a new domain to your organization.
resource "deno_domain" "example" {
  domain = "foo.example.com"
}

# Add DNS records to the nameserver.
resource "cloudflare_record" "dns_record" {
  for_each = {
    A     = deno_domain.example.dns_record_a
    AAAA  = deno_domain.example.dns_record_aaaa
    CNAME = deno_domain.example.dns_record_cname
  }

  zone_id = local.zone_id
  name    = each.value.name
  type    = upper(each.key)
  value   = each.value.content
  proxied = false
  ttl     = 120
}

The old attribute dns_record is now marked as deprecated but remains unchanged for compatibility. We may delete it before stabilizing the provider.

Fixes #17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for more ergonomic DNS records registration
1 participant