this is the error I'm getting:
Error: A resource with the ID "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Compute/virtualMachines/windows-web1" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_linux_virtual_machine" for more information.
on .terraform\modules\vm_windows_app1\vm\main.tf line 3, in resource "azurerm_linux_virtual_machine" "vm_linux":
3: resource "azurerm_linux_virtual_machine" "vm_linux" {
my terraform relevant folder structure is:
nonprod
|_ backend.tf
|_ outputs.tf
|_ provider.tf
|_ resource_groups.tf
|_ storage.tf
|_ subnets.tf
|_ variables.tf
|_ vnets.tf
|_ vms_lin.tf
|_ vms_win.tf
modules
|_ resource_group
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ storage
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ network
|_ vnet
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ subnet
|_ outputs.tf
|_ variables.tf
|_ main.tf
|_ vm
|_ data.tf
|_ main.tf
|_ nic.tf
|_ outputs.tf
|_ variables.tf
nonprod-vms_lin.tf
module "vm_linux_app1" {
source = "git::ssh://[email protected]/v3/sampleuser/my_code/terraform_modules//vm"
name = module.rg_app1.rg_name
location = module.rg_app1.rg_location
vnet_name = module.vnet_app1.vnet_name
subnet_name = module.subnet1_app1.subnet_name
base_hostname = "linux-web1"
admin_username = "azureuser"
admin_password = "W3lcomeWorld12!!"
caching_option = "ReadWrite"
storage_account_type = "Standard_LRS"
vm_size = "Standard_B1s"
vm_publisher = "Canonical"
vm_offer = "UbuntuServer"
vm_sku = "18.04-LTS"
vm_img_version = "latest"
}
nonprod-vms_win.tf
module "vm_windows_app1" {
source = "git::ssh://[email protected]/v3/sampleuser/my_code/terraform_modules//vm"
name = module.rg_app1.rg_name
location = module.rg_app1.rg_location
vnet_name = module.vnet_app1.vnet_name
subnet_name = module.subnet1_app1.subnet_name
base_hostname = "windows-web1"
admin_username = "azureuser"
admin_password = "W3lcomeWorld12!!"
caching_option = "ReadWrite"
storage_account_type = "Standard_LRS"
vm_size = "Standard_B1s"
vm_publisher = "MicrosoftWindowsServer"
vm_offer = "WindowsServer"
vm_sku = "2019-Datacenter"
vm_img_version = "latest"
}
modules-vm-main.tf
# Create the Virtual Machine
resource "azurerm_linux_virtual_machine" "vm_linux" {
name = var.base_hostname
location = var.location
resource_group_name = var.name
network_interface_ids = azurerm_network_interface.vm_nic.*.id
size = var.vm_size
##### azurerm_key_vault_secret data source to store password
admin_username = var.admin_username
admin_password = var.admin_password
disable_password_authentication = false
source_image_reference {
publisher = var.vm_publisher
offer = var.vm_offer
sku = var.vm_sku
version = var.vm_img_version
}
os_disk {
caching = var.caching_option
storage_account_type = var.storage_account_type
}
}
resource "azurerm_windows_virtual_machine" "vm_windows" {
name = var.base_hostname
location = var.location
resource_group_name = var.name
network_interface_ids =azurerm_network_interface.vm_nic.*.id
size = var.vm_size
admin_username = var.admin_username
admin_password = var.admin_password
source_image_reference {
publisher = var.vm_publisher
offer = var.vm_offer
sku = var.vm_sku
version = var.vm_img_version
}
os_disk {
caching = var.caching_option
storage_account_type = var.storage_account_type
}
}
I comment out all code in vms_lin.tf and run terraform plan/apply, it then creates the Win VM successfully.
Then I decomment all code in vms_lin.tf to provision the Linux VM and run terraform plan:
Terraform will perform the following actions:
# module.vm_linux_app1.azurerm_linux_virtual_machine.vm_linux will be created
+ resource "azurerm_linux_virtual_machine" "vm_linux" {
+ admin_password = (sensitive value)
+ admin_username = "azureuser"
+ allow_extension_operations = true
+ computer_name = (known after apply)
+ disable_password_authentication = false
+ id = (known after apply)
+ location = "australiasoutheast"
+ max_bid_price = -1
+ name = "linux-web1"
+ network_interface_ids = (known after apply)
+ priority = "Regular"
+ private_ip_address = (known after apply)
+ private_ip_addresses = (known after apply)
+ provision_vm_agent = true
+ public_ip_address = (known after apply)
+ public_ip_addresses = (known after apply)
+ resource_group_name = "tf-nonprod"
+ size = "Standard_B1s"
+ virtual_machine_id = (known after apply)
+ zone = (known after apply)
+ os_disk {
+ caching = "ReadWrite"
+ disk_size_gb = (known after apply)
+ name = (known after apply)
+ storage_account_type = "Standard_LRS"
+ write_accelerator_enabled = false
}
+ source_image_reference {
+ offer = "UbuntuServer"
+ publisher = "Canonical"
+ sku = "18.04-LTS"
+ version = "latest"
}
}
# module.vm_linux_app1.azurerm_network_interface.vm_nic will be created
+ resource "azurerm_network_interface" "vm_nic" {
+ applied_dns_servers = (known after apply)
+ dns_servers = (known after apply)
+ enable_accelerated_networking = false
+ enable_ip_forwarding = false
+ id = (known after apply)
+ internal_dns_name_label = (known after apply)
+ internal_domain_name_suffix = (known after apply)
+ location = "australiasoutheast"
+ mac_address = (known after apply)
+ name = "linux-web1-nic"
+ private_ip_address = (known after apply)
+ private_ip_addresses = (known after apply)
+ resource_group_name = "tf-nonprod"
+ virtual_machine_id = (known after apply)
+ ip_configuration {
+ name = "nic-pip"
+ primary = (known after apply)
+ private_ip_address = (known after apply)
+ private_ip_address_allocation = "dynamic"
+ private_ip_address_version = "IPv4"
+ public_ip_address_id = (known after apply)
+ subnet_id = "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Network/virtualNetworks/vnet-app1/subnets/subnet1"
}
}
# module.vm_linux_app1.azurerm_public_ip.vm_pip will be created
+ resource "azurerm_public_ip" "vm_pip" {
+ allocation_method = "Dynamic"
+ fqdn = (known after apply)
+ id = (known after apply)
+ idle_timeout_in_minutes = 4
+ ip_address = (known after apply)
+ ip_version = "IPv4"
+ location = "australiasoutheast"
+ name = "linux-web1-pip"
+ resource_group_name = "tf-nonprod"
+ sku = "Basic"
}
# module.vm_linux_app1.azurerm_windows_virtual_machine.vm_windows will be created
+ resource "azurerm_windows_virtual_machine" "vm_windows" {
+ admin_password = (sensitive value)
+ admin_username = "azureuser"
+ allow_extension_operations = true
+ computer_name = (known after apply)
+ enable_automatic_updates = true
+ id = (known after apply)
+ location = "australiasoutheast"
+ max_bid_price = -1
+ name = "linux-web1"
+ network_interface_ids = (known after apply)
+ priority = "Regular"
+ private_ip_address = (known after apply)
+ private_ip_addresses = (known after apply)
+ provision_vm_agent = true
+ public_ip_address = (known after apply)
+ public_ip_addresses = (known after apply)
+ resource_group_name = "tf-nonprod"
+ size = "Standard_B1s"
+ virtual_machine_id = (known after apply)
+ zone = (known after apply)
+ os_disk {
+ caching = "ReadWrite"
+ disk_size_gb = (known after apply)
+ name = (known after apply)
+ storage_account_type = "Standard_LRS"
+ write_accelerator_enabled = false
}
+ source_image_reference {
+ offer = "UbuntuServer"
+ publisher = "Canonical"
+ sku = "18.04-LTS"
+ version = "latest"
}
}
# module.vm_windows_app1.azurerm_linux_virtual_machine.vm_linux will be created
+ resource "azurerm_linux_virtual_machine" "vm_linux" {
+ admin_password = (sensitive value)
+ admin_username = "azureuser"
+ allow_extension_operations = true
+ computer_name = (known after apply)
+ disable_password_authentication = false
+ id = (known after apply)
+ location = "australiasoutheast"
+ max_bid_price = -1
+ name = "windows-web1"
+ network_interface_ids = [
+ "/subscriptions/subid/resourceGroups/tf-nonprod/providers/Microsoft.Network/networkInterfaces/windows-web1-nic",
]
+ priority = "Regular"
+ private_ip_address = (known after apply)
+ private_ip_addresses = (known after apply)
+ provision_vm_agent = true
+ public_ip_address = (known after apply)
+ public_ip_addresses = (known after apply)
+ resource_group_name = "tf-nonprod"
+ size = "Standard_B1s"
+ virtual_machine_id = (known after apply)
+ zone = (known after apply)
+ os_disk {
+ caching = "ReadWrite"
+ disk_size_gb = (known after apply)
+ name = (known after apply)
+ storage_account_type = "Standard_LRS"
+ write_accelerator_enabled = false
}
+ source_image_reference {
+ offer = "WindowsServer"
+ publisher = "MicrosoftWindowsServer"
+ sku = "2019-Datacenter"
+ version = "latest"
}
}
Plan: 5 to add, 0 to change, 0 to destroy.
------------------------------------------------------------------------
It's trying to re-create the Win VM again... and it's mashing code from the Linux/Win resources together it seems.
I'm still pretty new to Terraform, I would rather try to get this working rather than separating my Linux/Win Modules into 2 folders.
How should I go about this?
Any assistance would be appreciated :)
Your module calls both the linux and windows resource without a conditional. Typically a flag is passed or some method to determine if you want a windows vm or a linux vm. This flag would be assigned to count on the resource using a ternary operator return 1 or 0 to build it or not build it. I recommend you break the module up into a separate module.