Why can't I create Azure VMs with Terraform Sm

2019-08-17 14:04发布

问题:

I got below error message from command prompt after around 17 minutes. Also it seems too long for execution? or is it an expected duration?

azurerm_virtual_machine.vm: Still creating... (17m30s elapsed) Releasing state lock. This may take a few moments...

Error: Error applying plan:

1 error(s) occurred:

  • azurerm_virtual_machine.vm: 1 error(s) occurred:

  • azurerm_virtual_machine.vm: unexpected EOF

Terraform does not automatically rollback in the face of errors. Instead, your Terraform state file has been partially updated with any resources that successfully completed. Please address the error above and apply again to incrementally change your infrastructure.

Once checked inside portal, it's still showing "Creating..." status. I used below .tf file. Any issues in it?

provider "azurerm" {}

variable "location" {
  default = "East US"
}

variable "username" {
  default = "..."
}

variable "password" {
  default = "..."
}

resource "azurerm_resource_group" "resourceGroup" {
  name     = "TerraformResearchResourceGroup"
  location = "${var.location}"
}

resource "azurerm_public_ip" "publicip" {
  name                         = "terraformresearchpublicip"
  location                     = "${var.location}"
  resource_group_name          = "${azurerm_resource_group.resourceGroup.name}"
  public_ip_address_allocation = "Dynamic"
  idle_timeout_in_minutes      = 30

  tags {
    environment = "test"
  }
}

resource "azurerm_virtual_network" "vnet" {
  name                = "terraformresearchnetwork"
  address_space       = ["10.0.0.0/16"]
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.resourceGroup.name}"
}

resource "azurerm_subnet" "subnet" {
  name                 = "terraformresearchsubnet"
  resource_group_name  = "${azurerm_resource_group.resourceGroup.name}"
  virtual_network_name = "${azurerm_virtual_network.vnet.name}"
  address_prefix       = "10.0.2.0/24"
}

resource "azurerm_network_interface" "nic" {
  name                = "terraformresearchnic"
  location            = "${var.location}"
  resource_group_name = "${azurerm_resource_group.resourceGroup.name}"

  ip_configuration {
    name                          = "terraformresearchconfiguration"
    subnet_id                     = "${azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id          = "${azurerm_public_ip.publicip.id}"
  }
}

resource "azurerm_storage_account" "storageacc" {
  name                     = "terraformresearchstoacc"
  resource_group_name      = "${azurerm_resource_group.resourceGroup.name}"
  location                 = "${var.location}"
  account_tier             = "Standard"
  account_replication_type = "GRS"
}

resource "azurerm_storage_container" "storagecont" {
  name                  = "terraformresearchstoragecont"
  resource_group_name   = "${azurerm_resource_group.resourceGroup.name}"
  storage_account_name  = "${azurerm_storage_account.storageacc.name}"
  container_access_type = "private"
}

resource "azurerm_managed_disk" "datadisk" {
  name                 = "terraformresearchdatadisk"
  location             = "${var.location}"
  resource_group_name  = "${azurerm_resource_group.resourceGroup.name}"
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  disk_size_gb         = "1023"
}

resource "azurerm_virtual_machine" "vm" {
  name                  = "terraformrvm"
  location              = "${var.location}"
  resource_group_name   = "${azurerm_resource_group.resourceGroup.name}"
  network_interface_ids = ["${azurerm_network_interface.nic.id}"]
  vm_size               = "Standard_A0"

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }

  storage_os_disk {
    name              = "terraformresearhosdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  storage_data_disk {
    name            = "${azurerm_managed_disk.datadisk.name}"
    managed_disk_id = "${azurerm_managed_disk.datadisk.id}"
    create_option   = "Attach"
    lun             = 1
    disk_size_gb    = "${azurerm_managed_disk.datadisk.disk_size_gb}"
  }

  os_profile {
    computer_name  = "terraformrvm"
    admin_username = "${var.username}"
    admin_password = "${var.password}"
  }

  os_profile_windows_config {
    enable_automatic_upgrades = false
    provision_vm_agent = true
  }
}

回答1:

For your issue, I take a test and there is something needed to change in your .tf file.

First, the storage account name needs to be less or equal to 20 lengths.

Second, your VM size is too small so it will cost a long time to create the VM. If you change to a bigger size and the time will be shortened.

The last thing is that the username and password should be defined in the variables. I think you know it.