In this blog post, I will show you how to create multiple Windows virtual machines in Microsoft Azure using Terraform.
To create multiple Windows Machines, I will use the original Terraform configuration for creating a single VM and add the count argument to give each VM a different name (including the NIC).
Configuration
The full configuration is listed below. Make sure you set the password for the VM.
terraform { required_providers { azurerm = { source = "hashicorp/azurerm" version = "2.41.0" } } } provider "azurerm" { } resource "azurerm_resource_group" "rg" { name = "TFRDemo" location = "westus" } resource "azurerm_virtual_network" "rg" { name = "rg-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" "rg" { name = "internal" resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.rg.name address_prefixes = ["10.0.2.0/24"] } resource "azurerm_network_interface" "rg" { count = 2 name = "AZ-VM-00-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.rg.id private_ip_address_allocation = "Dynamic" } } resource "azurerm_windows_virtual_machine" "rg" { count = 2 name = "AZ-VM-00-${count.index}" resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location size = "Standard_F2" admin_username = "SETUSERNAME" admin_password = "SETPASSWORD network_interface_ids = [ azurerm_network_interface.rg.*.id[count.index], ] os_disk { caching = "ReadWrite" storage_account_type = "Standard_LRS" } source_image_reference { publisher = "MicrosoftWindowsServer" offer = "WindowsServer" sku = "2016-Datacenter" version = "latest" } }
Comments
2 responses to “Create Multiple Azure Windows VMs With Terraform”
Error in the code – missing “
Hi,
Thanks for the post.
Are you able to provide an example to create multiple VM using count index and join them to the domain afterward?