FHeinke
FHeinke

Reputation: 11

Issues with creating multiple vm's with Terraform for Azure

I am having issues with creating multiple virtual machines in Azure using Terraform. While creating the network interfaces I run into an error regarding the creation of the public ip address id:

Error message

I assume that I am using the count function incorrectly, or need a different approach entirely.

Code:

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
}

Can anyone help me resolve this issue??

Upvotes: 0

Views: 2234

Answers (1)

mjgpy3
mjgpy3

Reputation: 8957

I'm pretty sure all you need to do is change

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

to

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

In general, references like azurerm_public_ip.publicip.id work for singular resources (i.e. those that don't use count). So the use of element is kind of assuming a singular resource. As soon as count is used, resources start behaving like lists and need to be treated as such.

Upvotes: 2

Related Questions