Deploying a Windows Azure VM with Hashicorp Terraform to Microsoft Azure

Share on:

As a followup to my previous post on Automating EC2 deployments, I figured why not show how this can also be done with Microsoft Azure? Check out how to launch an Azure VM instance and run some scripts to customize your installation.

Overview

When it comes to automating deployments, its great to be able to use the same automation tool across clouds. This blog will focus on how to deploy a windows based Azure VM and run some custom scripts against it. My use case like my previous blog post is to run some custom scripts to configure ISCSI. Azure makes it easy to run remote scripts instead of adding the raw code. The attached example here will install choclately (windows package manager) and then install Notepad++. With this framework, you can have it do whatever you can automate with Powershell!

My example uses data blocks for the Azure Resource Group, Virtual Network and Subnet as these were already created. If you wish to have it create these resources, change them from data to resource blocks and it will require a few more variables.

Pre-Requisites

Azure CLI

  • This is used to authenticate to Azure to deploy the VM via Terraform.

Hashicorp Terraform

  • This is used to automate the provisioning using a Terraform .TF file.

Microsoft Azure Account

  • This is the infrastructure to run the Azure virtual machines.

Terraform Manifest Configuration

Download the sample manifest from GitHub and update the variables for your environment. This includes the Azure Resource Group, Virtual Network, Subnet and Interface. You will also need to update the Azure VM’s hostname, admin username and password as well as any of fields you wish.

 1provider "azurerm" {
 2    features {}
 3}
 4
 5data "azurerm_resource_group" "resourcegroup" {
 6    name     = "Azure-ResourceGroup"
 7}
 8
 9data "azurerm_virtual_network" "virtualnetwork" {
10    name                = "Azure-VirtualNetwork"
11    resource_group_name = data.azurerm_resource_group.resourcegroup.name
12}
13
14data "azurerm_subnet" "subnet" {
15    name                 = "Azure-Subnet"
16    resource_group_name  = data.azurerm_resource_group.resourcegroup.name
17    virtual_network_name = data.azurerm_virtual_network.virtualnetwork.name
18}
19
20resource "azurerm_network_interface" "networkinterface" {
21    name                = "Azure-NetworkInterface"
22    location            = data.azurerm_resource_group.resourcegroup.location
23    resource_group_name = data.azurerm_resource_group.resourcegroup.name
24    ip_configuration {
25        name = "Azure-IP"
26        subnet_id = data.azurerm_subnet.subnet.id
27        private_ip_address_allocation = "Dynamic"
28    }
29}
30
31resource "azurerm_windows_virtual_machine" "avm" {
32    name = "DS-TERRAFORM"
33    resource_group_name = data.azurerm_resource_group.resourcegroup.name
34    location = data.azurerm_resource_group.resourcegroup.location
35    computer_name = "hostname"
36    admin_username = "terraform"
37    admin_password = "Password1!"
38    size = "Standard_B1s"
39    network_interface_ids = [
40        azurerm_network_interface.networkinterface.id,
41    ]
42    os_disk {
43        caching              = "ReadWrite"
44        storage_account_type = "Standard_LRS"
45    }
46    source_image_reference {
47        publisher = "MicrosoftWindowsServer"
48        offer     = "WindowsServer"
49        sku       = "2019-Datacenter"
50        version   = "latest"
51    }
52}
53
54resource "azurerm_virtual_machine_extension" "customize" {
55    name                 = "customize"
56    virtual_machine_id   = azurerm_windows_virtual_machine.avm.id
57    publisher            = "Microsoft.Compute"
58    type                 = "CustomScriptExtension"
59    type_handler_version = "1.9"
60    protected_settings = <<PROTECTED_SETTINGS
61
62    protected_settings = <<PROTECTED_SETTINGS
63    {
64        "commandToExecute": "powershell.exe -Command \"./chocolatey.ps1; exit 0;\""
65    }
66    PROTECTED_SETTINGS
67
68    settings = <<SETTINGS
69    {
70        "fileUris": [
71            "https://gist.githubusercontent.com/mcasperson/c815ac880df481418ff2e199ea1d0a46/raw/5d4fc583b28ecb27807d8ba90ec5f636387b00a3/chocolatey.ps1"
72        ]
73    }
74    SETTINGS
75}

Deploy your Azure VM

Unlike with other Terraform providers where you specify login credentials in the manifest, Azure is a bit different. There are 4 options and the easiest is to authenticate using Azure CLI

To login just run the below command. It will open a web browser and you’ll authenticate.

1az login

Once authenticated, you can run your Terraform deployment.

Run terraform init to install any needed providers, terraform plan to make sure all the connectivity is working and then terraform apply to deploy!

1terraform init
2terraform plan
3terraform apply

If everything is successful your Azure Virtual Machine instance should be deployed in ~5minutes!

Closing

Hopefully this helped you get started with automating Azure VM deployment with Terraform!

Any questions or comments? Leave them below.

comments powered by Disqus

See Also