Reputation: 365
I am building a set of instances using a variable that is passed to instances, then instances needs to output a list of the hostnames and IPs that are known after creation. I created the output as a list object to allow route53 to build the records using count.index
I tried several ways as a a k,v map but ran into the same problem. I would like to be a list object, but the error is...
│ on main.tf line 33, in module "route_53":
│ 33: host_ips = module.instance.host_ips
│ ├────────────────
│ │ module.instance is a list of object, known only after apply
│
│ Can't access attributes on a list of objects. Did you mean to access attribute "host_ips" for a specific element of the list, or across all elements of the list?
I have tried with and without the type definer in rout53/variables.tf but it does not seem to mater, or it does not get that far.
main.tf
module "instance" {
source = "./instance"
count = length(var.instances)
instances = var.instances
sgs = module.security.sgs
}
module "route_53" {
source = "./route_53"
count = length(var.instances)
host_ips = module.instance.host_ips
}
instance/instance.tf
resource "aws_instance" "linux" {
ami = var.machine_image[var.instances[count.index].distro].ami
count = length(var.instances)
instance_type = var.instances[count.index].instance_type
monitoring = false
vpc_security_group_ids = [ for sg in var.instances[count.index].security_groups: var.sgs[sg] ]
associate_public_ip_address = true
source_dest_check = true
disable_api_termination = false
root_block_device {
volume_type = "gp2"
volume_size = var.instances[count.index].volume_size
delete_on_termination = false
}
tags = {
"Name" = var.instances[count.index].hostname
}
}
output "host_ips" {
value = [ for hip in aws_instance.linux[*]:
{
hostname = hip.tags["Name"]
pubic_ip = hip.public_ip
}
]
}
route53/variables.tf
variable "instances" {
default = "The nova.json instances"
}
variable "host_ips" {
# type = list(object({
# hostname = string
# public_ip = string
# }))
description = "FQDNs & Public facing IP addresses from Instances"
}
route53/record.tf
resource "aws_route53_record" "public" {
count = length(var.host_ips)
name = var.host_ips[count.index].hostname
records = [ var.host_ips[count.index].pubic_ip ]
ttl = 300
type = "A"
zone_id = aws_route53_zone.public_zone.zone_id
allow_overwrite = true
}
Upvotes: 0
Views: 2456
Reputation: 238847
Since you are using count
in the instance module, you have to use splat expression to explicitly use index to access its instances:
host_ips = module.instance[count.index].host_ips
Upvotes: 1