Benjamin
Benjamin

Reputation: 3826

Terraform - Invalid index error with multiple count

I am trying to configure 2 Public IP address, with 2 Network interface. What I have written so far is :

resource "azurerm_public_ip" "example" {
  name                    = "test-pip${count.index}"
  count = 2
  location                = "${azurerm_resource_group.rc.location}"
  resource_group_name     = "${azurerm_resource_group.rc.name}"
  allocation_method       = "Dynamic"
  idle_timeout_in_minutes = 30
}
output "public_ip_address" {
  value = "${azurerm_public_ip.example.*.id}"
}

resource "azurerm_network_interface" "main" {
  name                = "test${count.index}"
  count = 2
  location            = "${azurerm_resource_group.rc.location}"
  resource_group_name = "${azurerm_resource_group.rc.name}"

    ip_configuration {
    name                          = "testconfiguration1${count.index}"
    subnet_id                     = "${azurerm_subnet.internal.id}"
    private_ip_address_allocation = "Dynamic"
    public_ip_address_id          = "${azurerm_public_ip.example[count.index].id}"
  }
}

Later I will use these two IP, and NI to assign them to 2 VM machines.

When I run terraform plan , I get an error saying :

enter image description here

Terraform version is "v0.12.3" and azure provider version is "v1.40.0"

Upvotes: 3

Views: 17643

Answers (3)

you can try to change the code like this,It works for me:

public_ip_address_id = "${element(azurerm_public_ip.example.*.id, count.index)}"

Upvotes: 0

Charles Xu
Charles Xu

Reputation: 31424

Actually, I think there is nothing wrong in the Terraform code that you provided in the question and all things work fine on my side.

The error also said:

The given key does not identity an element this collection value.

It probably because that your public IPs do not created before the network interface. It's strange. Terraform will sequence all the resources in the right sequence. Maybe you can try to upgrade the Terraform version. What I used is the newest version:

Terraform v0.12.19
+ provider.azurerm v1.41.0

Or you can try to change the code like this:

public_ip_address_id    = "${element(azurerm_public_ip.example.*.id, count.index)}"

Upvotes: 2

Matthew Schuchard
Matthew Schuchard

Reputation: 28774

Given that this is Terraform 0.12 and not Terraform 0.11 as the question syntax implies, the actual error is in the specific exported attribute. To access the ip address exported by the azurerm_public_ip.example resource, we would need to use the ip_address exported attribute and not the id. This is why the error is being thrown for an invalid key, although the specific reference in the error is indeed misleading.

We can update your code to fix this by:

ip_configuration {
  name                          = "testconfiguration1${count.index}"
  subnet_id                     = "${azurerm_subnet.internal.id}"
  private_ip_address_allocation = "Dynamic"
  public_ip_address_id          = "${azurerm_public_ip.example[count.index].ip_address}"
}

Upvotes: 1

Related Questions