这是我用来创建子网和 nsg 的代码我想在同一个脚本中关联 NSG 和子网,但我无法理解如何获取此处生成的子网 ID 和 NSG ID 并在关联中使用它们资源。在此先感谢您的帮助 !
用于创建 n 个子网和 NSG的代码的第一部分取决于参数
provider "azurerm" {
version = "2.0.0"
features {}
}
resource "azurerm_resource_group" "new-rg" {
name = var.rg_name
location = "West Europe"
}
resource "azurerm_virtual_network" "new-vnet" {
name = var.vnet_name
address_space = ["${var.vnet_address_space}"]
location = azurerm_resource_group.new-rg.location
resource_group_name = azurerm_resource_group.new-rg.name
}
resource "azurerm_subnet" "test" {
count = "${length(var.subnet_prefix)}"
name = "${element(var.subnet_subnetname, count.index)}"
resource_group_name = azurerm_resource_group.new-rg.name
virtual_network_name = azurerm_virtual_network.new-vnet.name
address_prefix = "${element(var.subnet_prefix, count.index)}"
}
resource "azurerm_network_security_group" "new-nsg" {
count = "${length(var.subnet_prefix)}"
name = "${element(var.subnet_subnetname, count.index)}-nsg"
location = azurerm_resource_group.new-rg.location
resource_group_name = azurerm_resource_group.new-rg.name
}
下面是我必须传递参数以创建上述子网和正在创建的 nsg 的关联的资源。
代码的第二部分需要使以下代码可用于上述解决方案的 n 个关联。
resource "azurerm_subnet_network_security_group_association" "example" {
subnet_id = azurerm_subnet.example.id
network_security_group_id = azurerm_network_security_group.example.id
}
如何将 n 个子网和使用第二部分代码创建的 nsg 关联起来,我找不到我的方法