I need to create multiple DNS with their respected IPs. 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. But getting both of the IPs are getting assigned for both DNS(dns1 and dns2).Any suggestions?
Code:
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 = 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(list(string))
default = {
"dev" = [ "10.1.20.70", "10.1.20.140" ]
"qa" = [ "10.1.22.180", "10.1.22.150" ]
"test" = [ "10.1.23.190", "10.1.23.160" ]
}
}
Output
resource "aws_route53_record" "onprem_api_record" {
allow_overwrite = (known after apply)
fqdn = (known after apply)
id = (known after apply)
name = "dns1.dev.ciscodcloud.com"
records = [
"10.1.20.40",
"10.1.20.70",
]
ttl = 300
type = "A"
zone_id = "Z30HW9VL6PYDXQ"
}
aws_route53_record.onprem_api_record["dna2.dev.cisco.com"] will be created
resource "aws_route53_record" "onprem_api_record" {
allow_overwrite = (known after apply)
fqdn = (known after apply)
id = (known after apply)
name = "dns2.dev.cisco.com"
records = [
"10.1.20.40",
"10.1.20.70",
]
ttl = 300
type = "A"
zone_id = "Z30HW9VL6PYDXQ"
}
Plan: 2 to add, 0 to change, 1 to destroy.
CodePudding user response:
You can do this as follows with count
:
resource "aws_route53_record" "onprem_api_record" {
count = length(local.vm_fqdn)
zone_id = data.aws_route53_zone.dns_zone.zone_id
name = local.vm_fqdn[count.index]
type = "A"
records = [var.api_ips[terraform.workspace][count.index]]
ttl = "300"
}
CodePudding user response:
You may want to use zipmap. Here is a terse example showing its use in for_each
with for
as could be used in your case.
resource "aws_route53_record" "onprem_api_record" {
for_each = { for fqdn, ip in zipmap(local.vm_fqdn, local.ips["dev"]) : fqdn => ip }
zone_id = "x"
name = each.key
type = "A"
records = [each.value]
ttl = "300"
}
locals {
ips = {
"dev" = ["10.1.20.70", "10.1.20.140"]
"qa" = ["10.1.22.180", "10.1.22.150"]
"test" = ["10.1.23.190", "10.1.23.160"]
}
vm_fqdn = ["dns1-dev.domain", "dns2-dev.domain"]
}
And the plan looks like:
# aws_route53_record.onprem_api_record["dns1-dev.domain"] will be created
resource "aws_route53_record" "onprem_api_record" {
allow_overwrite = (known after apply)
fqdn = (known after apply)
id = (known after apply)
name = "dns1-dev.domain"
records = [
"10.1.20.70",
]
ttl = 300
type = "A"
zone_id = "x"
}
# aws_route53_record.onprem_api_record["dns2-dev.domain"] will be created
resource "aws_route53_record" "onprem_api_record" {
allow_overwrite = (known after apply)
fqdn = (known after apply)
id = (known after apply)
name = "dns2-dev.domain"
records = [
"10.1.20.140",
]
ttl = 300
type = "A"
zone_id = "x"
}
Plan: 2 to add, 0 to change, 0 to destroy.