Getting error during VM creation

Hi
I am trying to create virtual machine. But I am getting an error.
Capture

I am sending you my code. Can you guys check this code on your end and tell me what is the issue. Thanks

terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “3.80.0”
}
}
}

provider “azurerm” {

features {}

client_id = “”
client_secret = “”
tenant_id = “”
subscription_id = “”
}

resource “azurerm_resource_group” “rg10” {
name = “rg10”
location = “West Europe”
}

resource “azurerm_virtual_network” “virtual_network” {
name = “vnet01”
address_space = [“10.0.0.0/16”]
location = “West Europe”
resource_group_name = “rg10”
depends_on = [ azurerm_resource_group.rg10 ]
}

resource “azurerm_subnet” “vnet_subnet” {
name = “subnet01”
resource_group_name = “rg10”
virtual_network_name = “vnet01”
address_prefixes = [“10.0.2.0/24”]

}

resource “azurerm_network_interface” “vm_interface” {
name = “interface01”
location = “West Europe”
resource_group_name = “rg10”

depends_on = [ azurerm_virtual_network.virtual_network ]

ip_configuration {
name = “testconfiguration1”
subnet_id = data.azurerm_subnet.subnet.id
private_ip_address_allocation = “Dynamic”
}
}

data “azurerm_subnet” “subnet” {
name = “subnet01”
virtual_network_name = “vnet01”
resource_group_name = “rg10”
}

resource “azurerm_virtual_machine” “virtual_machine” {
name = “vm01”
location = “West Europe”
resource_group_name = “rg10”
network_interface_ids = [azurerm_network_interface.vm_interface.id]
vm_size = “Standard_D2S_v3”

storage_image_reference {
publisher = “MicrosoftWindowsServer”
offer = “WindowsServer”
sku = “2019-Datacenter”
version = “latest”
}

storage_os_disk {
name = “myosdisk1”
caching = “ReadWrite”
managed_disk_type = “Standard_LRS”
create_option = “FromImage”
}

os_profile {
computer_name = “vm01”
admin_username = “testadmin”
admin_password = “Password1234@”
}
}

Regards

Hi @hassan_darus,

From your code, you have a resource block for creating by the subnet and then you have a data block for sourcing that same subnet inside the same module.

Well, let’s first understand terraform flow of operations, terraform will first run all the GET data blocks. Note that data block is sourcing existing resource. Secondly, terraform will run all the PUT resource blocks.

So in your case, the data source was ran first to get a subnet you haven’t created in the first place.

Now, you’ll need to remove the data block for creating subnet, change the data.azurerm_subnet.subnet.id to azurerm_subnet.vnet_subnet.id in the subnet_id attribute of your resource block for azurerm_network_interface.

The above change will create that implicit dependency which allows the subnet to be created first for the network interface creation to use it.

See what your complete code will look like below.

terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “3.80.0”
}
}
}

provider “azurerm” {

features {}

client_id = “”
client_secret = “”
tenant_id = “”
subscription_id = “”
}

resource “azurerm_resource_group” “rg10” {
name = “rg10”
location = “West Europe”
}

resource “azurerm_virtual_network” “virtual_network” {
name = “vnet01”
address_space = [“10.0.0.0/16”]
location = “West Europe”
resource_group_name = azurerm_resource_group.rg10.name
}

resource “azurerm_subnet” “vnet_subnet” {
name = “subnet01”
resource_group_name = azurerm_virtual_network.virtual_network.resource_group_name
virtual_network_name = azurerm_virtual_network.virtual_network.id
address_prefixes = [“10.0.2.0/24”]

}

resource “azurerm_network_interface” “vm_interface” {
name = “interface01”
location = “West Europe”
resource_group_name = azurerm_resource_group.rg10.name

ip_configuration {
name = “testconfiguration1”
subnet_id = azurerm_subnet.vnet_subnet.id
private_ip_address_allocation = “Dynamic”
}
}

resource “azurerm_virtual_machine” “virtual_machine” {
name = “vm01”
location = “West Europe”
resource_group_name = azurerm_resource_group.rg10.name
network_interface_ids = [azurerm_network_interface.vm_interface.id]
vm_size = “Standard_D2S_v3”

storage_image_reference {
publisher = “MicrosoftWindowsServer”
offer = “WindowsServer”
sku = “2019-Datacenter”
version = “latest”
}

storage_os_disk {
name = “myosdisk1”
caching = “ReadWrite”
managed_disk_type = “Standard_LRS”
create_option = “FromImage”
}

os_profile {
computer_name = “vm01”
admin_username = “testadmin”
admin_password = “Password1234@”
}
}