Terraform: azurerm_network_interface.subnet_id: how to interpolate reference to an implicit subnet?

Created on 17 Feb 2016  ยท  7Comments  ยท  Source: hashicorp/terraform

If we define our subnets "implicitly" as below...

https://www.terraform.io/docs/providers/azurerm/r/virtual_network.html
resource "azurerm_virtual_network" "test" {
  name                = "virtualNetwork1"
  resource_group_name = "${azurerm_resource_group.test.name}"
  address_space       = ["10.0.0.0/16"]
  location            = "West US"

  subnet {
    name           = "subnet1"
    address_prefix = "10.0.1.0/24"
  }
}

... how do we interpolate/generate the required ref for azurerm_network_interface.ip_configuration.subnet_id?

The only hint for that is below, but only for subnets created "explicitly" via azurerm_subnet, not "implicitly" as part of "azurerm_virtual_network".

https://github.com/hashicorp/terraform/blob/7897e083424f4c5eb2e2fa012f794e36a06dfd29/builtin/providers/azurerm/resource_arm_network_interface_card_test.go
...
resource "azurerm_subnet" "test" {
    name = "testsubnet"
    resource_group_name = "${azurerm_resource_group.test.name}"
    virtual_network_name = "${azurerm_virtual_network.test.name}"
    address_prefix = "10.0.2.0/24"
}
resource "azurerm_network_interface" "test" {
    name = "acceptanceTestNetworkInterface1"
    location = "West US"
    resource_group_name = "${azurerm_resource_group.test.name}"
    ip_configuration {
        name = "testconfiguration1"
        subnet_id = "${azurerm_subnet.test.id}"
        private_ip_address_allocation = "dynamic"
    }
}

provideazurerm question

All 7 comments

Good question, @joaocc! @stack72 can you chime in here?

I've tried a number of different configurations, but I haven't been able to figure this one out. I learned that you're required to include at least one subnet (or the vnet creation fails), so I've been creating a "junk" subnet, then creating specific azurerm_subnets for the ones I need to access later on.

Edit: I re-read your post, and you're doing what I ended up doing. :)

@stack72 @phinze Any guidance for this question? I also had to create a separate subnet resource in order to get the subnet ID for the network interface.

uhmm, same here. Looks like we cannot refer from the "subnet" instead of "azurerm_subnet".
"azurerm_subnet" is a dedicated resource, but "subnet" is a property inside of "azurerm_virtual_network", also it can be multiple implemented in one resource as below:

`
resource "azurerm_virtual_network" "au_network" {
name = "au_network"
resource_group_name = "${azurerm_resource_group.au_prod.name}"
address_space = ["192.168.0.0/16"]
location = "${azurerm_resource_group.au_prod.location}"
dns_servers = ["8.8.8.8", "8.8.4.4"]

subnet {
    name            = "au_subnet0"
    address_prefix  = "192.168.0.0/24"
}

subnet {
    name            = "au_subnet1"
    address_prefix  = "192.168.1.0/24"
}

# subnet {
#   name            = "au_subnet2"
#   address_prefix  = "192.168.2.0/24"
# }

}

resource "azurerm_subnet" "au_subnet2" {
name = "au_subnet2"
resource_group_name = "${azurerm_resource_group.au_prod.name}"
virtual_network_name = "${azurerm_virtual_network.au_network.name}"
address_prefix = "192.168.2.0/24"
}
`

In order to get the right 'id' for 'au_subnet2', looks like using "azurerm_subnet" as the resource is the only way to get the 'id'.

River

This is correct. If you believe this is incorrect behavior please do open a new issue stating the bug!

Any update on this topic ?
In order to get a subnet ID (and reference it in a NIC), is it still required to declare it explicitly ?

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukehoersten picture lukehoersten  ยท  151Comments

mirogta picture mirogta  ยท  74Comments

glenjamin picture glenjamin  ยท  112Comments

kklipsch picture kklipsch  ยท  95Comments

oillio picture oillio  ยท  78Comments