Reputation: 19
What resources can be nested within another resource in Azure terraform?
Can we nest the creation of resource: network_interface / IP_configuration within a VM deployment (virtual_machine) even though virtual_machine contains the parameter: network_interface_ids ? Can I assume it is best to place network_interface / IP configurations outside this VM nest?
More importantly, how do we know what resources can be nested and which resources cannot be nested?
Upvotes: 1
Views: 350
Reputation: 3372
Usually resources in Terraform are not nested. The documentation typically covers if there is the potential to do inlining of resources, which is rare.
Typically, you create all your resources at the same level, then reference the NIC ids or IP configurations inside of your Virtual Machine. The other doesn't matter either in which you define these in your Terraform file. Terraform will build out a depedency graph for you to determine the order that resources need created.
You can see this in the example from the docs:
resource "azurerm_network_interface" "main" {
name = "${var.prefix}-nic"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.internal.id}"
private_ip_address_allocation = "Dynamic"
}
}
resource "azurerm_virtual_machine" "main" {
name = "${var.prefix}-vm"
location = "${azurerm_resource_group.main.location}"
resource_group_name = "${azurerm_resource_group.main.name}"
network_interface_ids = ["${azurerm_network_interface.main.id}"]
vm_size = "Standard_DS1_v2"
# Adding Availability zones
zones = [1]
}
Here is the link to the example.
Upvotes: 1