0

Below is the template I have for azure VM.

In Google cloud, we have option to set count for creating multiple machines, as I heard.

How to create multiple machines using a single template, so that based on variable value, those many number of machines should be created.

Sample template for azure windows server VM.

github url: link

I want to keep this repo permanently public, so not posting the direct files here.

4

2 Answers 2

2

One way you could achieve this is to declare properties as variables and use them as arguments for for_each inside the resource definition.

See example here: https://stackoverflow.com/a/64462458/11942781

0

Here is a rough working example of how you could use the meta argument "count" with azurerm_windows_virtual_machine:

provider "azurerm" {
  features {}
}
resource "random_string" "username" { length = 8 }
resource "random_password" "password" { length = 24 }

resource "azurerm_resource_group" "rg" {
  name     = "count-test-win"
  location = "northeurope"
}
# Set the count of virtual machines you want
variable "vm_count" {
  default = 4
}

resource "azurerm_virtual_network" "test" {
  name                = "test-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

resource "azurerm_subnet" "test" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.rg.name
  virtual_network_name = azurerm_virtual_network.test.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "nic" {
  count               = var.vm_count
  name                = "nic-${count.index}"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.test.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_windows_virtual_machine" "vm" {
  count                 = var.vm_count
  name                  = "win-vm-${count.index}"
  resource_group_name   = azurerm_resource_group.rg.name
  location              = azurerm_resource_group.rg.location
  size                  = "Standard_F2"
  admin_username        = random_string.username.result
  admin_password        = random_password.password.result
  network_interface_ids = [azurerm_network_interface.nic[count.index].id]

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  source_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter"
    version   = "latest"
  }
}

Though I would suggest that you look into writing your own module or look at using Virtual Machine Scale Sets and determine if its a better fit for your use-case at https://docs.microsoft.com/en-us/azure/virtual-machine-scale-sets/overview

The related azurerm module documentation for the windows version, can be found at https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/windows_virtual_machine_scale_set

Not the answer you're looking for? Browse other questions tagged or ask your own question.