【问题标题】:Issues with creating multiple vm's with Terraform for Azure使用 Terraform for Azure 创建多个 vm 的问题
【发布时间】:2020-07-04 20:49:10
【问题描述】:

我在使用 Terraform 在 Azure 中创建多个虚拟机时遇到问题。在创建网络接口时,我遇到了关于创建公共 IP 地址 id 的错误:

我假设我错误地使用了 count 函数,或者完全需要一种不同的方法。

代码:

provider "azurerm" {
  version = "~>2.0"
  features {}

  subscription_id = "XXXX"
  client_id       = "XXXX"
  client_secret   = "XXXX"
  tenant_id       = "XXXX"
}

resource "azurerm_resource_group" "rg" {
    name        = "${var.prefix}test_project"
    location    = var.location
    tags        = var.tags
}

resource "azurerm_virtual_network" "vnet" {
    name                = "${var.prefix}Vnet"
    address_space       = ["10.0.0.0/16"]
    location            = var.location
    resource_group_name = azurerm_resource_group.rg.name
    tags                = var.tags
}

resource "azurerm_subnet" "subnet" {
    name                 = "${var.prefix}Subnet"
    resource_group_name  = azurerm_resource_group.rg.name
    virtual_network_name = azurerm_virtual_network.vnet.name
    address_prefix       = "10.0.1.0/24"
}

resource "azurerm_public_ip" "publicip" {
    name                    = "${var.prefix}PublicIP${count.index}"
    location                = var.location
    resource_group_name     = azurerm_resource_group.rg.name
    allocation_method       = "Dynamic"
    tags                    = var.tags
    count                   = 2
}

resource "azurerm_network_security_group" "nsg" {
    name                = "${var.prefix}NetworkSecurityGroup"
    location            = var.location
    resource_group_name = azurerm_resource_group.rg.name
    tags                = var.tags

    security_rule {
        name                       = "SSH"
        priority                   = 1001
        direction                  = "Inbound"
        access                     = "Allow"
        protocol                   = "Tcp"
        source_port_range          = "*"
        destination_port_range     = "22"
        source_address_prefix      = "*"
        destination_address_prefix = "*"
    }
}

resource "azurerm_network_interface" "nic" {
    name                        = "${var.prefix}NIC${count.index}"
    location                    = var.location
    resource_group_name         = azurerm_resource_group.rg.name
    tags                        = var.tags
    count                       = 2

    ip_configuration {
        name                          = "${var.prefix}NICConfig${count.index}"
        subnet_id                     = azurerm_subnet.subnet.id
        private_ip_address_allocation = "Dynamic"
        public_ip_address_id          = ["${element(azurerm_public_ip.publicip.id, count.index)}"]
    }
}

resource "azurerm_network_interface_security_group_association" "example" {
    count = length(azurerm_network_interface.nic)
    network_interface_id      = "${azurerm_network_interface.nic[count.index]}"
    network_security_group_id = azurerm_network_security_group.nsg.id
}

resource "azurerm_linux_virtual_machine" "vm" {
    count                   = 2
    name                    = "${var.prefix}VM${count.index}"
    location                = var.location
    resource_group_name     = azurerm_resource_group.rg.name
    network_interface_ids   = azurerm_network_interface.nic[count.index]
    size                    = "Standard_DS1_v2"
    tags                    = var.tags

    os_disk {
        name                    = "${var.prefix}OsDisk${count.index}"
        caching                 = "ReadWrite"
        storage_account_type    = "Premium_LRS"
    }

    source_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = lookup(var.sku, var.location)
        version   = "latest"
    }

    computer_name  = "${var.computer_name}-${count.index}"
    admin_username = var.admin_username
    admin_password = var.admin_password
    disable_password_authentication = false
}

谁能帮我解决这个问题?

【问题讨论】:

  • 您的错误应该直接以文本形式出现在问题中,而不是作为链接的屏幕截图。

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


【解决方案1】:

我很确定你需要做的就是改变

        public_ip_address_id          = ["${element(azurerm_public_ip.publicip.id, count.index)}"]

到

        public_ip_address_id          = ["${azurerm_public_ip.publicip[count.index].id}"]

一般来说,像azurerm_public_ip.publicip.id 这样的引用适用于单一资源(即那些不使用计数的资源)。所以element 的使用有点假设一个单一的资源。一旦使用了count,资源就会开始表现得像列表一样,需要被这样对待。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 2019-07-14
    • 2017-03-25
    • 2022-10-02
    • 2018-02-28
    • 2020-03-21
    • 1970-01-01
    • 2021-05-30
    • 1970-01-01
    相关资源
    最近更新 更多