【问题标题】:Why can't I create Azure VMs with Terraform anymore?为什么我不能再使用 Terraform 创建 Azure VM?
【发布时间】:2018-02-13 12:56:21
【问题描述】:

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

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 = "..."

    }


}

【问题讨论】:

  • 你能找出真正的错误吗?您也可以尝试使用this docker image 来测试带有 azure(或其他图像)的 terraform。
  • @4c74356b41 真正的错误是什么意思?这是我从 Terraform 得到的错误,据我所知,我无法从 Azure 访问错误消息。我马上就去看看 docker 镜像,谢谢!
  • 好吧,为什么不能访问 Azure 错误?登录门户并自行查看
  • @GeoffreyGrosenbach 根据我的理解,Azure API 可能会发生变化,在创建 VNet 时,需要 sunbnet。看看我的回答,这适用于 ukwest。
  • @MattBrinkman 创建 VNet 时,需要子网,请参阅我的答案。

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


【解决方案1】:

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

# 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

【讨论】:

  • 我在我的实验室测试,我修改你的例子像this,这对我来说很好。
  • 这行得通!非常感谢 :) 对于 vnet 对子网及其自身的子网的引用中的名称和地址前缀有何不同,我感到有些困惑。为什么一个“tfsub”和另一个“subnet1”?我们不应该使用 ${} terraform 引用吗?
  • @ShengbaoShui-MSFT 如果这是真的,您能否解释一下为什么我能够在其他区域创建 vnet(同时使用 az cli 和 pwoershell) 创建子网? (我昨晚向 Azure 支持人员提供了演示此功能的屏幕截图。)今天早上,我还能够在美国西部创建没有子网的 vnet。
【解决方案2】:

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

【讨论】:

  • 不,这不是真的。根本原因是 OP 在创建 VNet 时没有包含子网。这是必需的。我测试 Azure CLI。这对我来说可以。 az network vnet create \ --name TestVNet \ --resource-group TestRG \ --location centralus \ --address-prefix 192.168.0.0/16 \ --subnet-name FrontEnd \ --subnet-prefix 192.168.1.0/24。是的,我在 ukwest 进行了测试。
猜你喜欢
  • 1970-01-01
  • 2019-07-14
  • 2022-10-02
  • 2020-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
相关资源
最近更新 更多