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

Allow for more ergonomic DNS records registration #17

Open
magurotuna opened this issue Oct 10, 2023 · 1 comment · May be fixed by #39
Open

Allow for more ergonomic DNS records registration #17

magurotuna opened this issue Oct 10, 2023 · 1 comment · May be fixed by #39

Comments

@magurotuna
Copy link
Member

Now the provider users need to register DNS records like the following:

# 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
}

terraform's for_each doesn't work here; if we did that it would complain:

╷
│ Error: Invalid for_each argument
│ 
│   on main.tf line 77, in resource "cloudflare_record" "my_record":
│   77:   for_each = toset(deno_domain.example_domain.dns_records)
│     ├────────────────
│     │ deno_domain.example_domain.dns_records is a list of object, known only after apply
│ 
│ The "for_each" set includes values derived from resource attributes that
│ cannot be determined until apply, and so Terraform cannot determine the
│ full set of keys that will identify the instances of this resource.
│ 
│ When working with unknown values in for_each, it's better to use a map
│ value where the keys are defined statically in your configuration and
│ where only the values contain apply-time results.
│ 
│ Alternatively, you could use the -target planning option to first apply
│ only the resources that the for_each value depends on, and then apply a
│ second time to fully converge.

We would like to look for a better solution to make it easier to write.

@w7it
Copy link

w7it commented Mar 15, 2024

You can use count meta-attribute to solve it:

resource "cloudflare_record" "deno_domain" {
  count = 3

  zone_id = "<put your zone ID>"
  name    = deno_domain.example.dns_records[count.index].name
  type    = upper(deno_domain.example.dns_records[count.index].type)
  value   = deno_domain.example.dns_records[count.index].content
  proxied = false
  ttl     = 120
}

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 a pull request may close this issue.

2 participants