Score:0

How to set logic to create multiple machines on azure using terraform?

cn flag

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.

cn flag
Start by having a look at the official documentation at https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
cn flag
And also read up on the count meta-argument at https://www.terraform.io/language/meta-arguments/count
Sara June avatar
cn flag
Thanks for the reply, as you can observe the details, that option seems to be available only for Google cloud, not for azure.
cn flag
The Meta arguments are available for all modules, so I am not sure I understand you. Hell there is even an example with Windows Servers here -> https://buildvirtual.net/terraform-count-examples/ ... You really need to edit your question with an example of what youre doing and what error you are experiencing.
Score:2
jp flag

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

Score:0
cn flag

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

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.