Hallo,
I want to create multiple identical vm's however, as I am unable to do: network_interface_ids = ["${azurerm_network_interface.network-interface-${count.index}.id}"] I cannot see a way of programatically creating many resources of the same time. Can anyone advise a way around this apparent limitation?
Currently I'm declaring every resource individually which get cumbersome very quickly! :)
resource "azurerm_public_ip" "public-ip-address-0" {
    name                 = "${azurerm_resource_group.resource_group.name}-public-ip-address-0"
    location = "${azurerm_resource_group.resource_group.location}"
    resource_group_name = "${azurerm_resource_group.resource_group.name}"
    public_ip_address_allocation = "dynamic"
}
resource "azurerm_public_ip" "public-ip-address-1" {
    name                 = "${azurerm_resource_group.resource_group.name}-public-ip-address-1"
    location = "${azurerm_resource_group.resource_group.location}"
    resource_group_name = "${azurerm_resource_group.resource_group.name}"
    public_ip_address_allocation = "dynamic"
}
resource "azurerm_network_interface" "network-interface-0" {
  name                 = "${azurerm_resource_group.resource_group.name}-network-interface-0"
  location            = "West Europe"
  resource_group_name = "${azurerm_resource_group.resource_group.name}"
  ip_configuration {
    name                 = "${azurerm_resource_group.resource_group.name}"
    subnet_id                     = "${azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id           = "${azurerm_public_ip.public-ip-address-0.id}"
  }
}
resource "azurerm_network_interface" "network-interface-1" {
  name                 = "${azurerm_resource_group.resource_group.name}-network-interface-1"
  location            = "West Europe"
  resource_group_name = "${azurerm_resource_group.resource_group.name}"
  ip_configuration {
    name                 = "${azurerm_resource_group.resource_group.name}"
    subnet_id                     = "${azurerm_subnet.subnet.id}"
    private_ip_address_allocation = "dynamic"
    public_ip_address_id           = "${azurerm_public_ip.public-ip-address-1.id}"
  }
}
resource "azurerm_virtual_machine" "fecore-0" {
  name                  = "${azurerm_resource_group.resource_group.name}-0"
  location              = "West Europe"
  resource_group_name   = "${azurerm_resource_group.resource_group.name}"
  network_interface_ids = ["${azurerm_network_interface.network-interface-0.id}"]
  vm_size               = "Standard_A2_v2"
  # count                 = 2
  storage_os_disk {
    name          = "${azurerm_resource_group.resource_group.name}-0"
    image_uri     = "https://foobar.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.11101831-8448-4c61-9fd6-74d6f718a2ff.vhd"
    vhd_uri       = "https://foobar.blob.core.windows.net/images/${azurerm_resource_group.resource_group.name}-0.vhd"
    os_type       = "linux"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }
  os_profile {
    computer_name  = "${azurerm_resource_group.resource_group.name}-0"
    admin_username = "centos"
    admin_password = "foobar"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags {
    environment = "staging"
  }
}
resource "azurerm_virtual_machine" "fecore-1" {
  name                  = "${azurerm_resource_group.resource_group.name}-1"
  location              = "West Europe"
  resource_group_name   = "${azurerm_resource_group.resource_group.name}"
  network_interface_ids = ["${azurerm_network_interface.network-interface-1.id}"]
  vm_size               = "Standard_A2_v2"
  # count                 = 2
  storage_os_disk {
    name          = "${azurerm_resource_group.resource_group.name}-1"
    image_uri     = "https://foobar.blob.core.windows.net/system/Microsoft.Compute/Images/images/packer-osDisk.11101831-8448-4c61-9fd6-74d6f718a2ff.vhd"
    vhd_uri       = "https://foobar.blob.core.windows.net/images/${azurerm_resource_group.resource_group.name}-1.vhd"
    os_type       = "linux"
    caching       = "ReadWrite"
    create_option = "FromImage"
  }
  os_profile {
    computer_name  = "${azurerm_resource_group.resource_group.name}-1"
    admin_username = "centos"
    admin_password = "foobar"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags {
    environment = "staging"
  }
}
Hey @mooperd
Thanks for opening this issue :)
I want to create multiple identical vm's however, as I am unable to do: network_interface_ids = ["${azurerm_network_interface.network-interface-${count.index}.id}"] I cannot see a way of programatically creating many resources of the same time. Can anyone advise a way around this apparent limitation?
This is possible using the element interpolation function - as can be seen here:
${element(var.ip_addresses, count.index)}
Here's a full usage example showing creating multiple Network Interfaces with count:
variable "ip_addresses" {
  default = [
    "10.0.2.11",
    "10.0.2.12",
    "10.0.2.13",
  ]
}
resource "azurerm_resource_group" "test" {
  name     = "tharvey-dev"
  location = "West Europe"
}
resource "azurerm_virtual_network" "test" {
  name                = "test"
  address_space       = ["10.0.0.0/16"]
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_subnet" "test" {
  name                 = "internal"
  resource_group_name  = "${azurerm_resource_group.test.name}"
  virtual_network_name = "${azurerm_virtual_network.test.name}"
  address_prefix       = "10.0.2.0/24"
}
resource "azurerm_network_interface" "test" {
  name  = "nic${count.index}"
  count = "${length(var.ip_addresses)}"
  location            = "${azurerm_resource_group.test.location}"
  resource_group_name = "${azurerm_resource_group.test.name}"
  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = "${azurerm_subnet.test.id}"
    private_ip_address_allocation = "Static"
    private_ip_address            = "${element(var.ip_addresses, count.index)}"
  }
}
output "public_ips" {
  value = "${azurerm_network_interface.test.*.ip_address}"
}
output "first_address" {
  value = "${azurerm_network_interface.test.0.ip_address}"
}
Would you be able to take a look and see if that solves this issue for you?
I want to create multiple identical vm's
On a related note - if you're creating multiple identical VM's, have you considered the azurerm_virtual_machine_scale_set resource?
Thanks!
That's some good info. I'm still trying to get to grips with variables in terraform.
In the end this gives me an array of network interface objects which you are outputting with:
output "public_ips" {
  value = "${azurerm_network_interface.test.*.ip_address}"
}
If I have a similar array of Virtual Machines created in a similar way:
resource "azurerm_virtual_machine" "virtual-machines" {
  name                  = "${azurerm_resource_group.resource_group.name}-${count.index}"
  location              = "West Europe"
  resource_group_name   = "${azurerm_resource_group.resource_group.name}"
  network_interface_ids = ["${azurerm_network_interface.network-interface-1.id}"]
  vm_size               = "Standard_A2_v2"
  count                 =  10
}
How do I map "network_interface_ids" to ${azurerm_network_interface.test.*} so that each machine gets a network interface?
Scale sets are next on my list but for the moment I need to create some machines capable of developing a personality :)
@mooperd
That's some good info. I'm still trying to get to grips with variables in terraform.
No worries :)
There's a ton of information in the configuration section and some examples of count on this page which may help?
How do I map "network_interface_ids" to ${azurerm_network_interface.test.*} so that each machine gets a network interface?
Presuming your TF config looks similar to this:
# ...
resource "azurerm_network_interface" "network-interface" {
  name = "foo${count.index}"
  count = 3
  # ...
}
resource "azurerm_virtual_machine" "foo" {
  name = "foo${count.index}"
  count = 3
  # ...
}
# ...
.. you should be able to use the same interpolation function as in the previous example:
resource "azurerm_virtual_machine" "foo" {
  # ...
  count = 3
  network_interface_ids = ["${element(azurerm_network_interface.network-interface.*.id, count.index)}"]
  # ...
}
Would you be able to take a look and let me know how you get on? :)
Thanks!
๐ @mooperd - have you had a chance to take a look at this yet? :)
๐ hey @mooperd
Given we've not heard back from you here I'm going to close this issue for the moment. That said - if this is still an issue please let us know and we'll re-open and take another look :)
Thanks!
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error ๐ค ๐ , please reach out to my human friends ๐ [email protected]. Thanks!
Most helpful comment
๐ @mooperd - have you had a chance to take a look at this yet? :)