0

Not sure where things are going wrong but I am unable to get the following code working.

Aim: To create two (or more) virtual machine with public IP.

Issue: Stuck with terraform plan reporting erros as indicated in Error code block.

Terraform code block is below:

resource "azurerm_public_ip" "tf-pubip-cluster-aos" {
    count                        = 2
    name                         = "${var.ax_base_hostname}-${count.index+1}-PUBIP"
    location                     = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
    resource_group_name          = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
    allocation_method            = "Dynamic"
}

resource "azurerm_network_interface" "tf-ni-cluster-aos" {
 count               = 2
 name                = "${var.ax_base_hostname}-${count.index+1}-NI"
 location            = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
 resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"

ip_configuration {
    name                          = "${var.ax_base_hostname}-${count.index+1}-IP"
    subnet_id                     = "${data.azurerm_subnet.tf-sn-cluster-aos.id}"
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = "${azurerm_public_ip.tf-pubip-cluster-aos.id}"
}
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
  count                 = 2
  name                  = "${var.ax_base_hostname}-${count.index+1}"
  location            = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
  resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
  availability_set_id   = "${azurerm_availability_set.tf-as-cluster-aos.id}"
  network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index)}"]
  vm_size               = "${var.ax_vm_size}"

}

Error message is below:

Error running plan: 1 error(s) occurred:

    azurerm_network_interface.tf-ni-cluster-aos: 2 error(s) occurred:
    azurerm_network_interface.tf-ni-cluster-aos[0]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'
    azurerm_network_interface.tf-ni-cluster-aos[1]: Resource 'azurerm_public_ip.tf-pubip-cluster-aos' not found for variable 'azurerm_public_ip.tf-pubip-cluster-aos.id'

Couldn't figure it out... Any assistance will be great.

learner
  • 2,480
  • 10
  • 50
  • 94

1 Answers1

1

you create 2 public ips, not one, but you try and reference it like it was a single ip, but its not. its a list. you need to get individual public ip id, something like this:

"${element(azurerm_public_ip.tf-pubip-cluster-aos.*.id, count.index)}"
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thank you @4c74356b41 :) That is my mistake, I have used it before but I don't know what it really does? May I be allowed to ask, what .*.id,count.index really does? – learner Feb 24 '19 at 01:34
  • takes element number x from the list of id's. you can read the docs about element function – 4c74356b41 Feb 24 '19 at 05:59
  • The * in the expression "resource_name.*.id" is called the "splat" operator. It returns a list (array) of all resources created of a specific "resource_name" type without requiring to define the list variable in advance. Then, like any other list, you can access any element in the list using the "element(, )" function which receives 2 parameters 1. List 2. Index (Numbered location in list) – J00MZ Feb 25 '19 at 08:51