【问题标题】:How to reference count index in another module?如何在另一个模块中引用计数索引?
【发布时间】:2021-09-08 22:17:24
【问题描述】:

我已经使用 count.index 部署了两个子网,然后我需要在 subnet_route_table_association 模块中引用子网的 ID。谁能告诉我正确的方法是什么?

这是我的代码

/application/demo/main.tf

module “subnet_association” {
source = “…/…/Modules/subnet_association”
subid = var.subid
subnet_id = module.subnet.subnet_id
route_table_id = module.route_table.route_table_id
}

模块/子网/main.tf

resource "azurerm_subnet" "module-spoke-subnet" {
  count                                          = var.subnet_count
  name                                           = element(var.subnet_name, count.index)
  resource_group_name                            = var.resource_group_name
  virtual_network_name                           = var.virtual_network_name
  address_prefixes                               = [var.subnet_address[count.index]]
  enforce_private_link_endpoint_network_policies = true
  enforce_private_link_service_network_policies  = true
}
variable "resource_group_name" {
}
variable "virtual_network_name" {
}

模块/子网/输出.tf

output "subnet_id" {
  value = azurerm_subnet.module-spoke-subnet.*.id
}

模块/subnet_association/main.tf

resource "azurerm_subnet_route_table_association" "module-subnet-association" {
  subnet_id      = var.subnet_id
  route_table_id = var.route_table_id
}
variable "subnet_id" {

}
variable "route_table_id" {
}

我得到了错误

 Error: Incorrect attribute value type
│
│   on ..\..\modules\Subnet_association\main.tf line 20, in resource "azurerm_subnet_route_table_association" "module-subnet-association":
│   20:   subnet_id      = var.subnet_id
│     ├────────────────
│     │ var.subnet_id is tuple with 1 element
│
│ Inappropriate value for attribute "subnet_id": string required.

【问题讨论】:

    标签: terraform


    【解决方案1】:

    输出 subnet_id 是一个元素列表。因此,如果您想为每个子网创建关联,请尝试以下操作:

    resource "azurerm_subnet_route_table_association" "module-subnet-association" {
    count = length(module.subnet[*].subnet_id)
      subnet_id      = module.subnet[count.index]. subnet_id
      route_table_id = var.route_table_id
    }
    

    或者,如果您只创建一个子网,请尝试以下操作:

    resource "azurerm_subnet_route_table_association" "module-subnet-association" {
      subnet_id      = module.subnet[0]. subnet_id
      route_table_id = var.route_table_id
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-18
      • 2015-02-11
      • 2013-05-08
      • 1970-01-01
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多