Creating a Azure Windows VM through Terraform

10,990

Solution 1

From Terraform's perspective, a Windows VM is really quite similar to a Linux VM. The #1 difference in my opinion is that the Windows VM requires a os_profile_windows_config attribute, while the Linux VM needs os_profile_linux_config.

The TF code you found on the Microsoft site is a fine start. Additionally, you may look in the Terraform Registry. For example, here's a module for a Linux VM.

I strongly recommend reading through all of the options in the VM resource. I know it's a lot, but you should understand what choices you have.

Lastly, there's no substitute for writing some code and testing it. If you do something wrong, either Terraform and/or the Azure API will tell you, and if it's unclear, a web search will pop up an answer or a pointer in the right direction.

Solution 2

Below is an example of how to use data to use already existing resources in terraform, also there is a code block to create a windows VM. You will need to get the existing VNET and create a NIC

Use the data directive to get the VNET azurerm_virtual_network, you can see the syntax below for the resource group. You will need to add the resource group and possibly location into this block.

Create a azurerm_network_interface resource using the VNET ID

Add the network interface ID to the VM (network_interface_ids = [])

Example TF Code to Create and load balance VMs

variable "subscription_id" {}
variable "client_id" {}
variable "client_secret" {}
variable "tenant_id" {}

provider "azurerm" {
  tenant_id       = "${var.tenant_id}"
  subscription_id = "${var.subscription_id}"
  client_id       = "${var.client_id}"
  client_secret   = "${var.client_secret}"
}

data "azurerm_resource_group" "resource_group" {
  name                = "learning-tf-web-rg"
}


resource "azurerm_virtual_machine" "web_server" {
  name                  = "server"
  location              = "westus2"
  resource_group_name   = "${data.azurerm_resource_group.resource_group.name}"
  network_interface_ids = []
  vm_size               = "Standard_B2s"

  storage_image_reference {
    publisher = "MicrosoftWindowsServer"
    offer     = "WindowsServer"
    sku       = "2016-Datacenter-Server-Core-smalldisk"
    version   = "latest"
  }

  storage_os_disk {
    name              = "server-os"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name      = "server"
    admin_username     = "server"
    admin_password     = "Passw0rd1234"

  }

  os_profile_windows_config {
  }

}
Share:
10,990
CrazyCoder
Author by

CrazyCoder

Updated on June 05, 2022

Comments

  • CrazyCoder
    CrazyCoder almost 2 years

    In Azure, I'm trying to create a Windows VM using Terraform. I have done this through Powershell previously using Template.json file. Now I have to do with terraform, which I'm completely new to. So I have searched for some Sample scripts which creates VM in Azure and found this.

    In this link, there is a sample Terraform script to spin a Linux VM. But I need to spin a windows VM from an Image. Where should I give the Image details. My complete requirement is:

    1. Create a Windows VM from an Image (have resource Id)
    2. I already have Resource group, Virtual network, Subnet created. I just need to pass those values and create them.
    3. We have already defined the Subnet address prefix, Vnet address space from the portal itself. So do I have to give again in the script or can I skip it.
    4. The business requirement is that no VMs should have public IP and DNS name, So if I remove "# Create public IPs" section, will that not create public IP?

    The script for creating a Linux machine is here, which I'm taking it as reference.