Am trying to create multiple DNS with appropriate IP in each workspace( like in dev workspace dns1 - 10.1.20.70 and dns2-10.1.20.40) . I’ve tried using the below method but getting the following error. Any suggestion?
resource "aws_route53_record" "onprem_api_record" {
for_each = toset(local.vm_fqdn)
zone_id = data.aws_route53_zone.dns_zone.zone_id
name = each.value
type = "A"
records = [lookup(var.api_ips, terraform.workspace)]
ttl = "300"
}
locals {
vm_fqdn = flatten(["dns1-${terraform.workspace}.${local.domain}", "dns2-${terraform.workspace}.${local.domain}"] )
}
variable "api_ips" {
type = map(any)
default = {
"dev" = [ "10.1.20.70", "10.1.20.40" ]
"qa" = [ "10.1.22.80", "10.1.22.50" ]
"test" = [ "10.1.23.90", "10.1.23.60" ]
}
}
Error:
Error: Incorrect attribute value type
on dns.tf line 26, in resource "aws_route53_record" "onprem_api_record":
26: records = [lookup(var.api_ips, terraform.workspace)]
|----------------
| terraform.workspace is "dev"
| var.dcloud_api_ips is object with 3 attributes
Inappropriate value for attribute "records": element 0: string required.
Error: Incorrect attribute value type
on dns.tf line 26, in resource "aws_route53_record" "onprem_api_record":
26: records = [lookup(var.api_ips, terraform.workspace)]
|----------------
| terraform.workspace is "dev"
| var.dcloud_api_ips is object with 3 attributes
Inappropriate value for attribute "records": element 0: string required.
Thanks a lot for the reply @alisdair. I’ve tried using the above code and getting the following output. Both the IP’s are getting assigned for both of the DNS(dns1 and dns2). I need to assign the first ip to the first DNS and the 2nd one to 2nd DNS. something like dns1 - 10.1.20.70 and dns2-10.1.20.40. Any suggestions ?
Code:
Just a word of caution, DNS Is complex enough and causes enough problems as it is, and you’re breaking some of the major no-no-rules of DNS by doing this.
To update two different DNS servers with two different IPs (doesn’t matter if it’s the same host) then you would need to define them separately. I’m sure there is a trickery way of doing it with using the index of the server and the index of ip to get around this but that’s just going to cause issues down the line if anyone ever tries to figure out what in world is going on. I don’t recommend it.
@aram Thanks for pointing me to the link, @apparentlymart suggested using count in that case. Is that possible to use for_each for my case?.. I’ve tried to follow @alisdair suggestion but no luck so far. Still getting the same above output (Both DNS getting two IP’s).