hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.61k forks source link

azurerm_windows_virtual_machine fails with apply when using modules #13609

Open JanVidarElven opened 2 years ago

JanVidarElven commented 2 years ago

Community Note

Terraform (and AzureRM Provider) Version

TerraForm 1.0.8, AzureRM Provider version 2.79.1

Affected Resource(s)

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file. For
# security, you can also encrypt the files using our GPG public key: https://keybase.io/hashicorp
variable "fas_ip" {
  type = map(string)
  default = {
    vmthfa01 = "172.18.196.36"
    vmthfa02 = "172.18.196.37"
  }
}

resource "azurerm_availability_set" "AvailabilitySetFAS" {
  name                = "as-authservices-fas"
  location            = var.azure_region
  resource_group_name = azurerm_resource_group.authservicesresourcegroup.name
  managed             = true
  platform_fault_domain_count = 2 # East Norway support max 2
  platform_update_domain_count = 5

  tags = azurerm_resource_group.authservicesresourcegroup.tags
} 

module "windows-server-fas" {
  for_each = var.fas_ip
  source = "git::https://REDACTED@dev.azure.com/REDACTED/REDACTED/_git/REDACTED%20Modules//REDACTED-windows-server"

  azure_region = var.azure_region
  resource_group_name = azurerm_resource_group.authservicesresourcegroup.name
  server_name = each.key
  server_size = "Standard_D4s_v4"
  subnet_id = data.azurerm_subnet.subnetSpokeAuthServices.id
  server_ip = each.value
  os_disk_type = "StandardSSD_LRS"
  os_license_type = "None"
  os_patch_mode = "Manual"
  server_availability_set_id = azurerm_availability_set.AvailabilitySetFAS.id

  deployment_tags = azurerm_resource_group.authservicesresourcegroup.tags
}

Module Configuration

# Module Reference for Deployment Assets
module "deploy-assets" {
  source = "./modules/deploy-assets"
}

# Create NIC for VMs
resource "azurerm_network_interface" "nicvfkvm" {
    name                      = "nic-${var.server_name}-1"
    location                  = var.azure_region
    resource_group_name       = var.resource_group_name

    ip_configuration {
        name                          = "niccfg-${var.server_name}"
        subnet_id                     = var.subnet_id
        private_ip_address_allocation = "static"
        private_ip_address            = var.server_ip
    }

    tags = var.deployment_tags

}

# Create Windows VM 
resource "azurerm_windows_virtual_machine" "vm" {
    name                = var.server_name
    location            = var.azure_region
    resource_group_name = var.resource_group_name
    size                = var.server_size
    admin_username      = module.deploy-assets.admin_username_value
    admin_password      = module.deploy-assets.admin_password_value
    network_interface_ids = [azurerm_network_interface.nicvfkvm.id]

    computer_name       = var.server_name

    availability_set_id = var.server_availability_set_id

    os_disk {
        name              = "mdisk-${var.server_name}-os"
        caching           = "ReadWrite"
        storage_account_type = var.os_disk_type
    }

    source_image_reference {
        publisher = var.image_publisher
        offer     = var.image_offer
        sku       = var.image_sku
        version   = "latest"
    }

    boot_diagnostics {
        storage_account_uri = module.deploy-assets.diagstorageaccount_uri
    }

    provision_vm_agent = true
    enable_automatic_updates = var.os_enable_automatic_updates
    patch_mode = var.os_patch_mode
    license_type = var.os_license_type
    timezone = "W. Europe Standard Time"

    tags = var.deployment_tags

}

Debug Output

Panic Output

Expected Behaviour

Windows Virtual Machine should be created successfully. I have been using this module config many times for this project, only last few weeks this has been starting to error.

Actual Behaviour

Every VM tries to create for over 10 minutes before this is returned in VS Code: │ Error: creating Windows Virtual Machine "vmthfa02" (Resource Group "rg-authservices"): compute.VirtualMachinesClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="InternalOperationError" Message="An internal error occurred in the operation." │ │ with module.windows-server-fas["vmthfa02"].azurerm_windows_virtual_machine.vm, │ on .terraform\modules\windows-server-fas\windows-server\server.tf line 24, in resource "azurerm_windows_virtual_machine" "vfkvm": │ 24: resource "azurerm_windows_virtual_machine" "vm" {

There is nothing more of info that helps me find where the error is.

Steps to Reproduce

  1. terraform apply

Important Factoids

It should be noted that if I try to create this server in TerraForm without using a module, just manually specifiying the exact same values it works.

References

ljluestc commented 2 days ago

resource "azurerm_windows_virtual_machine" "vm" {
    name                = "vmthfa02" # Simplified for testing
    location            = var.azure_region
    resource_group_name = azurerm_resource_group.authservicesresourcegroup.name
    size                = "Standard_D4s_v4"
    admin_username      = "adminuser"
    admin_password      = "P@ssw0rd123" # Use a secure password or use key vault for storing it

    network_interface_ids = [azurerm_network_interface.nicvfkvm.id]

    os_disk {
        name              = "mdisk-${var.server_name}-os"
        caching           = "ReadWrite"
        storage_account_type = "StandardSSD_LRS"
    }

    source_image_reference {
        publisher = "MicrosoftWindowsServer"
        offer     = "WindowsServer"
        sku       = "2019-Datacenter"
        version   = "latest"
    }

    boot_diagnostics {
        storage_account_uri = module.deploy-assets.diagstorageaccount_uri
    }

    provision_vm_agent = true
    enable_automatic_updates = false
    patch_mode = "Manual"
    license_type = "None"
    timezone = "W. Europe Standard Time"

    tags = var.deployment_tags
}