为什么我不能平滑地使用Terraform创建Azure VM

时间:2018-07-09 16:23:22

标签: .net azure terraform azure-rm

大约17分钟后,我从命令提示符处收到以下错误消息。还似乎执行时间太长了?还是预期的持续时间?

  

azurerm_virtual_machine.vm:仍在创建...(17分30秒)   释放状态锁。这可能需要一些时间...

     

错误:应用计划时出错:

     

发生1个错误:

     
      
  • azurerm_virtual_machine.vm:发生1个错误:

  •   
  • azurerm_virtual_machine.vm:意外的EOF

  •   
     

面对错误,Terraform不会自动回滚。   相反,您的Terraform状态文件已部分更新为   成功完成的资源。请解决以上错误   并再次应用以逐步更改您的基础架构。

一旦检查了门户内部,它仍然显示“正在创建...”状态。我在.tf文件下面使用过。有什么问题吗?

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 个答案:

答案 0 :(得分:1)

对于您的问题,我进行了测试,您的.tf文件中需要进行一些更改。

首先,存储帐户名称必须小于或等于20个长度。

第二,您的VM太小,因此创建VM会花费很长时间。如果更改为更大的尺寸,时间将会缩短。

最后一件事是,应在变量中定义用户名和密码。我想你知道。