繁体   English   中英

Terraform 将 output 列表 object 传递给另一个模块

[英]Terraform passing output list object to another module

我正在使用传递给实例的变量构建一组实例,然后实例需要 output 创建后已知的主机名和 IP 列表。 我创建了 output 作为列表 object 以允许 route53 使用count.index构建记录

我尝试了几种方法 aak,v map 但遇到了同样的问题。 我想成为一个列表object,但错误是...

│   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?

我已经尝试过在 rout53/variables.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
}

实例/实例.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
}

由于您在实例模块中使用count ,因此您必须使用 splat 表达式来显式使用 index 来访问其实例:

 host_ips        = module.instance[count.index].host_ips

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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