为什么我不能再使用Terraform创建Azure VM?

时间:2018-02-08 18:47:05

标签: azure terraform azure-virtual-network terraform-provider-azure

我一直在使用Terraform在Azure上部署VM。它一直运行良好,但今天我发现它在10分钟后尝试部署vnet时会超时,这在以前从未发生过。这是日志:

Error: Error applying plan:

1 error(s) occurred:

* azurerm_virtual_network.vnet: 1 error(s) occurred:

* azurerm_virtual_network.vnet: network.VirtualNetworksClient#CreateOrUpdate: Failure sending request: StatusCode=200 -- Original Error: Long running operation terminated with status 'Failed': Code="InternalServerError" Message="An error occurred."

我首先想到的是Terraform或azurerm插件错误,所以我尝试了几种组合(terraform 0.11.3 / azurerm 1.1.1,terraform 0.10.6 / azurerm 0.3.3等)。我和所有人都遇到了同样的问题。我可以毫无问题地从Azure门户创建一个VM,所以我想问题将出现在Terraform或Azure API Terraform的底层使用。在任何情况下,我都不知道如何调试它。

这是我正在使用的terraform模板:

# Configure Azure provider
provider "azurerm" {
  subscription_id = "..."
  client_id       = "..."
  client_secret   = "..."
  tenant_id       = "..."
}

# create a resource group if it doesn't exist
resource "azurerm_resource_group" "rg" {
    name = "a132rg"
    location = "ukwest"
}

# create virtual network
resource "azurerm_virtual_network" "vnet" {
    name = "tfvnet"
    address_space = ["10.0.0.0/16"]
    location = "ukwest"
    resource_group_name = "${azurerm_resource_group.rg.name}"
}

# create subnet
resource "azurerm_subnet" "subnet" {
    name = "tfsub"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    virtual_network_name = "${azurerm_virtual_network.vnet.name}"
    address_prefix = "10.0.2.0/24"
    #network_security_group_id = "${azurerm_network_security_group.nsg.id}"
}

# create public IPs
resource "azurerm_public_ip" "ip" {
    name = "tfip"
    location = "ukwest"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    public_ip_address_allocation = "dynamic"
    domain_name_label = "a132"

    tags {
        environment = "staging"
    }
}

# create network interface
resource "azurerm_network_interface" "ni" {
    name = "tfni"
    location = "ukwest"
    resource_group_name = "${azurerm_resource_group.rg.name}"

    ip_configuration {
        name = "ipconfiguration"
        subnet_id = "${azurerm_subnet.subnet.id}"
        private_ip_address_allocation = "static"
        private_ip_address = "10.0.2.5"
        public_ip_address_id = "${azurerm_public_ip.ip.id}"
    }
}

# create storage account
resource "azurerm_storage_account" "storage" {
    name = "0fda935368315bd1a5f5560e"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    location = "ukwest"
    account_replication_type = "LRS"
    account_tier = "Standard"

    tags {
        environment = "staging"
    }
}

# create storage container
resource "azurerm_storage_container" "storagecont" {
    name = "vhd"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    storage_account_name = "${azurerm_storage_account.storage.name}"
    container_access_type = "private"
    depends_on = ["azurerm_storage_account.storage"]
}



# create virtual machine
resource "azurerm_virtual_machine" "vm" {
    name = "a132vm"
    location = "ukwest"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    network_interface_ids = ["${azurerm_network_interface.ni.id}"]
    vm_size = "Standard_A6"

    storage_image_reference {
        publisher = "Canonical"
        offer = "UbuntuServer"
        sku = "16.04-LTS"
        version = "latest"
    }

    storage_os_disk {
        name = "myosdisk"
        vhd_uri = "${azurerm_storage_account.storage.primary_blob_endpoint}${azurerm_storage_container.storagecont.name}/myosdisk.vhd"
        caching = "ReadWrite"
        create_option = "FromImage"
    }

    os_profile {
        computer_name = "a132"
        admin_username = "..."

        admin_password = "..."

    }


}

2 个答案:

答案 0 :(得分:0)

创建VNet时,需要在其中创建子网。见link:VirtualNetworkPropertiesFormat object。所以,你需要修改你的文件,如下所示:

# create virtual network
resource "azurerm_virtual_network" "vnet" {
    name = "tfvnet"
    address_space = ["10.0.0.0/16"]
    location = "ukwest"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    ##You need create a subnet in VNet.
    subnet {
        name = "subnet1"
        address_prefix = "10.0.3.0/24"

    }
}

# create subnet
resource "azurerm_subnet" "subnet" {
    name = "tfsub"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    virtual_network_name = "${azurerm_virtual_network.vnet.name}"
    address_prefix = "10.0.2.0/24"
    #network_security_group_id = "${azurerm_network_security_group.nsg.id}"
}

有关此问题的详情,请参阅此link

答案 1 :(得分:-2)

该问题目前正在缓解。可以通过Portal为那些无法通过CLI或PowerShell创建VNET的区域创建VNET。