Reputation: 2860
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?
Upvotes: 3
Views: 6179
Reputation: 2860
I got it working by the below code:
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}"
Upvotes: 2
Reputation: 31454
As @BMW says, there is no property security_group
in the data azurerm_subnet
. If you want to associate an NSG to the existing subnet, you can use azurerm_subnet_network_security_group_association
to achieve that. Just use the data azurerm_subnet
to refer your existing subnet and create an NSG for it or use the existing one.
Upvotes: 3
Reputation: 45313
There is no key of security_group
in data source 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
Upvotes: 1