I need your help. We have created a resource group (rg_networking) and virtual network(vnet_preprod) and 5 subnets in that (subnet_ad), subnet_app, subnet_ctl etc.
My import of resources works perfectly fine, however, what I don't know is to use/reference the imported resources?
terraform import --var-file=aos-1.tfvars azurerm_virtual_network.vnet_preprod /subscriptions/00000000000000000/resourceGroups/rg_networking/providers/Microsoft.Network/virtualNetworks/vnet_preprod
azurerm_virtual_network.vnet_preprod: Importing from ID "/subscriptions/00000000000000000/resourceGroups/rg_networking/providers/Microsoft.Network/virtualNetworks/vnet_preprod"...
azurerm_virtual_network.vnet_preprod: Import complete!
Imported azurerm_virtual_network (ID: /subscriptions/00000000000000000/resourceGroups/rg_networking/providers/Microsoft.Network/virtualNetworks/vnet_preprod)
azurerm_virtual_network.vnet_preprod: Refreshing state... (ID: /subscriptions/00000000000000000-...rk/virtualNetworks/vnet_preprod)
Import successful!
The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.
In my windows-build-tf file
resource "azurerm_virtual_network" "vnet_preprod" {
name = ""
address_space = ""
location = ""
resource_group_name = ""
}
I had to insert the above snippet else import would not have worked.
My previous working configuration, with Terraform creating everything and not using any imported resources is below:
variables.tfvars
address_space = [ "10.97.0.0/16" ]
virtual_network_subnet_ad = "10.97.1.0/24"
virtual_network_subnet_ad_name="groups-preprod_subnet_ad"
virtual_network_nic_ad="groups-preprod_nic_ad"
build-windows.tf
resource "azurerm_virtual_network" "tf-virtual-network" {
name = "${var.virtual_network_name}"
address_space = "${var.address_space}"
location = "${var.Location}"
resource_group_name = "${var.Resource_group_name}"
}
resource "azurerm_subnet" "tf-virtual-network-subnet-ad" {
name = "${var.virtual_network_subnet_ad_name}"
resource_group_name = "${var.Resource_group_name}"
virtual_network_name = "${azurerm_virtual_network.tf-virtual-network.name}"
address_prefix = "${var.virtual_network_subnet_ad}"
}
resource "azurerm_network_interface" "tf-virtual-network-nic-ad" {
name = "${var.virtual_network_nic_ad}"
location = "${var.Location}"
resource_group_name = "${var.Resource_group_name}"
ip_configuration {
name = "testconfiguration1"
subnet_id = "${azurerm_subnet.tf-virtual-network-subnet-ad.id}"
private_ip_address_allocation = "dynamic"
}
}
resource "azurerm_virtual_machine" "tf-virtual-machine-name" {
name = "${var.virtual_machine_name}"
location = "${var.Location}"
resource_group_name = "${var.Resource_group_name}"
network_interface_ids = ["${azurerm_network_interface.tf-virtual-network-nic-ad.id}"]
vm_size = "Standard_DS3_v2"
}
My question is how to reference the imported resource, I prefer them to be parametric but if its not possible then hard coded values would be the way to move forward? do I need to create my VM in same resource group?
I can see them imported in state file, kindly guide as I'm very much new to Azure and Terraform.
Many Thanks!