I have a resource like below, I want all my record variable has to be mapped to same subdomain name in the resource, since I have already used count for another purpose I am not able to use count, is there a way to use multiple counts or is there any other way that I can achive this.
variable “records”{
default = example1.com,example2.com,example3.com
}
variable “subdomain_name” {
default = apps.com
}
resource “aws_route53_record” “database” {
count = var.enable ? 1 : 0
zone_id = var.private_zone_id
name = var.subdomain_name
type = “CNAME”
ttl = “300”
records = [var.records]
}
If using Terraform 0.12.6
+, I would recommend using a for_each
expression to iterate over a list. However, since count
and for_each
are mutually exclusive - we will not be able to have both expressions in place at once.
I don’t have any domains registered on AWS, so I had to test this approach with Cloudflare.
variable "records" {
default = ["terraform", "example", "third"]
type = list
}
resource "cloudflare_record" "dns" {
for_each = toset(var.records)
zone_id = var.cloudflare_zone_id
name = "subdomain"
value = "${each.value}"
type = "CNAME"
ttl = 3600
}
On second glance at the aws_route53_record
resource the records
argument takes a list. You could convert your comma seperated string variable records
, and pass records = var.records
instead of wrapping it in a list.