简体   繁体   中英

Terraform passing output list object to another module

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 aak,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
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM