使用Terraform for Azure创建多个虚拟机的问题

时间:2020-03-24 10:58:20

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

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

Error message

我假设我错误地使用了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
}

有人可以帮助我解决此问题吗?

1 个答案:

答案 0 :(得分:0)

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

        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之类的引用适用于单个资源(即那些不使用count的资源)。因此,element的使用是假设单个资源。一旦使用count,资源就会像列表一样开始工作,因此需要如此对待。