terraform assignment of public IPs to VM's using count meta-argument

Viewed 185

im trying to build a script that will build VMs based on count. i have managed to get most of it working, but the part im failing on is assigning the created public Ips to the Nic. the public ips are created but fail on the assignment, im not sure on how to parse correctly. Code and errors.

# Create public IPs
resource "azurerm_public_ip" "myterraformpublicip" {
   count = 2
   name                         = "myPublicIP-${count.index + 1}"
   location                     = "westeurope"
   resource_group_name          = azurerm_resource_group.myterraformgroup.name
   allocation_method            = "Dynamic"
}

# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
    count = 2
    name                      = "myNIC-${count.index + 1}"
    location                  = "westeurope"
    resource_group_name       = azurerm_resource_group.myterraformgroup.name

    ip_configuration {
      name                          = "myNicConfiguration"
      subnet_id                     = azurerm_subnet.myterraformsubnet.id
      private_ip_address_allocation = "Dynamic"
      public_ip_address_id          = "azurerm_network_interface.myterraformpublicip.[count.index + 1].id"
    }
}
 

#The error when running the plan.

Error: Can not parse "ip_configuration.0.public_ip_address_id" as a resource id: Cannot parse Azure ID: parse "element(azurerm_network_interface.myterraformpublicip.*.id, count.index + 1)": invalid URI for request

1 Answers

You can use something like below for your requirement:

provider "azurerm" {
    features{}
}

data "azurerm_resource_group" "test" {
  name = "yourresourcegroup"
}


resource "azurerm_virtual_network" "vnet" {
  name                = "ansuman--vnet"
  location            = data.azurerm_resource_group.test.location
  resource_group_name = data.azurerm_resource_group.test.name
  address_space       = ["10.0.0.0/16"]

}
variable "subnet_prefix" {
  type = list
  default = [
    {
      ip      = "10.0.1.0/24"
      name     = "subnet-1"
    },
    {
      ip      = "10.0.2.0/24"
      name     = "subnet-2"
    }
   ]
}

resource "azurerm_subnet" "test_subnet" {
    name = "${lookup(element(var.subnet_prefix, count.index), "name")}"
    count = "${length(var.subnet_prefix)}"
    resource_group_name = data.azurerm_resource_group.test.name
    virtual_network_name = azurerm_virtual_network.vnet.name
    address_prefix = "${lookup(element(var.subnet_prefix, count.index), "ip")}"
}

resource "azurerm_public_ip" "myterraformpublicip" {
   count = 2
   name                         = "myPublicIP-${count.index + 1}"
   location                     = data.azurerm_resource_group.test.location
   resource_group_name          = data.azurerm_resource_group.test.name
   allocation_method            = "Dynamic"
}
# Create network interface
resource "azurerm_network_interface" "myterraformnic" {
    count = 2
    name                      = "myNIC-${count.index + 1}"
    location                  = data.azurerm_resource_group.test.location
    resource_group_name       = data.azurerm_resource_group.test.name

    ip_configuration {
      name                          = "myNicConfiguration"
      subnet_id                     = azurerm_subnet.test_subnet[count.index].id
      private_ip_address_allocation = "Dynamic"
      public_ip_address_id          = azurerm_public_ip.myterraformpublicip[count.index].id
    }
}

Outputs:

enter image description here

enter image description here

enter image description here

enter image description here

Related