How to create a NSG to an existing subnet with terraform on azure

时间:2019-04-17 02:48:15

标签: terraform terraform-provider-azure

I am referencing existing subnet(s) as below, but I want a NSG to be created and attached to the subnet. It gives me error.

Code for refrencing and adding NSG is below:

data "azurerm_subnet" "tf-sn-erx-app" {
  name                 = "${var.subnet_app_name}"
  virtual_network_name = "${data.azurerm_virtual_network.tf-vn-erx.name}"
  resource_group_name  = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  security_group       = "${azurerm_network_security_group.tf-nsg-erx-application.id}"
}

data "azurerm_subnet" "tf-sn-erx-sql" {
  name                 = "${var.subnet_sql_name}"
  virtual_network_name = "${data.azurerm_virtual_network.tf-vn-erx.name}"
  resource_group_name  = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  security_group       = "${azurerm_network_security_group.tf-nsg-erx-sql.id}"
}
resource "azurerm_network_security_group" "tf-nsg-erx-application" {
  name                = "${var.application_nsg}"
  location            = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
  resource_group_name = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
}
resource "azurerm_network_security_rule" "tf-nsr-erx-application-5985" {
  name                        = "Open Port 5985"
  priority                    = 100
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "5985"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  network_security_group_name = "${azurerm_network_security_group.tf-nsg-erx-application.name}"
}
resource "azurerm_network_security_rule" "tf-nsr-erx-application-5986" {
  name                        = "Open Port 5986"
  priority                    = 101
  direction                   = "Inbound"
  access                      = "Allow"
  protocol                    = "Tcp"
  source_port_range           = "*"
  destination_port_range      = "5986"
  source_address_prefix       = "*"
  destination_address_prefix  = "*"
  resource_group_name         = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
  network_security_group_name = "${azurerm_network_security_group.tf-nsg-erx-application.name}"
}

However, when I do run, terraform, below error is reported.

Error: data.azurerm_subnet.tf-sn-erx-app: : invalid or unknown key: security_group
Error: data.azurerm_subnet.tf-sn-erx-sql: : invalid or unknown key: security_group

What is the issue?

3 个答案:

答案 0 :(得分:1)

数据源security_group中没有azurerm_subnet的密钥

Argument Reference
name - (Required) Specifies the name of the Subnet.
virtual_network_name - (Required) Specifies the name of the Virtual Network this Subnet is located within.
resource_group_name - (Required) Specifies the name of the resource group the Virtual Network is located in.

https://www.terraform.io/docs/providers/azurerm/d/subnet.html

答案 1 :(得分:1)

正如@BMW所说,数据security_group中没有属性azurerm_subnet。如果要将NSG与现有子网相关联,可以使用azurerm_subnet_network_security_group_association来实现。只需使用数据azurerm_subnet来引用您现有的子网并为其创建NSG或使用现有的子网即可。

答案 2 :(得分:0)

我通过以下代码使其工作:

resource "azurerm_network_interface" "tf-ni-erx-mkconn" {
 count                     = 3
 name                      = "${var.mkconn_base_hostname}${format("%02d",count.index+1)}-nic01"
 location                  = "${data.azurerm_resource_group.tf-rg-erx-external.location}"
 resource_group_name       = "${data.azurerm_resource_group.tf-rg-erx-external.name}"
 network_security_group_id = "${azurerm_network_security_group.tf-nsg-erx-application.id}"