How to Build with Terraform: Azure VMs (Windows)

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. In this guide we’ll walk through how to create an Azure VM with Terraform.

As the Azure cloud services provider, Terraform offers a variety of environments to build with. In this blog post I’ll show you how to use Windows VMs in your own development environment and manage them using terraform.

The “terraform azure windows vm example” is a tutorial that shows how to build with Terraform. The tutorial uses Azure VMs on Windows.

How to Build with Terraform: Azure VMs (Windows)

[*]

Have you ever established an Azure VM using the Azure portal? If that’s the case, how many clicks did it take? Can you repeat the process, supplying the exact same input numbers each time to verify that you reach a certain standard? Most likely not. You must automate the procedure. Terraform may be used to create Azure VMs.

Hashicorp’s Terraform is a tool for building basic to complicated infrastructure on-premises or in the cloud. Terraform is a single binary file that reads configuration files, establishes a state, and guarantees that your infrastructure produces and maintains that state.

You’ll learn how to get started with Terraform by building an Azure VM in this tutorial. This tutorial will serve as an excellent Terraform Azure demonstration.

Prerequisites

If you want to follow along with this tutorial, make sure you have the following items ready to go:

This article will use macOS with PowerShell Core throughout, however the same procedures can be performed on Windows 10 using PowerShell Core, Windows PowerShell, or the Windows command prompt.

Terraform Installation

To begin, you’ll need to first download Terraform. Depending on your preferences, there are a variety of methods to do this. You may use HomeBrew by executing brew install terraform on macOS in this tutorial. You may alternatively go straight to the Terraform download page, or use Chocolatey if you’re on Windows.

Did you know that Terraform is pre-installed in Azure Cloud Shell?

After you’ve downloaded Terraform, place it in a folder in your path and execute terraform. As indicated below, there should be some use instructions. You’re ready to go if you see this.

Instructions for Using TerraformInstructions for Using Terraform

Creating an Azure account

Terraform communicates with numerous on-premises and cloud suppliers through providers. You’ll need to set up the Terraform Azure provider in this situation. Assuming you’ve installed the Azure CLI and are already signed in to Azure, you’ll need to establish a service principal first. Terraform will login and get access to your Azure subscription using the service principal.

Make your own Service Principal.

In your console, Make your own Service Principal. using the Azure CLI. To do that:

To begin, use the az account list command to locate your subscription ID.

> az account list –query [*].[name,id]

Once you have the subscription ID, then Make your own Service Principal. using the Contributor role scoped to your subscription.

$subscriptionId = ‘xxxx-xxxxx-xxxxxXXXXXXXXXXXXXXXXXXXXXXXXXXX —role=”Contributor” —scopes=”/subscriptions/$subscriptionId” -n $sp = az ad sp create-for-rbac ConvertFrom-Json | TerraformTesting

As illustrated below, the Azure CLI will connect to Azure and build an Azure AD application using a password. Because you’re assigning the below output to the $sp variable, the appId and password will be saved in the variable for subsequent use.

Password-protected Azure ID applicationPassword-protected Azure ID application

Configure the Environment Variables

To connect to Azure, Terraform needs to know four distinct configuration elements.

  • The subscription ID for Azure
  • The Azure AD application ID of the service principal
  • The password for the service principal
  • The Azure Active Directory tenant

Using environment variables is one approach to deliver this information to Terraform. Because this course is being done in a PowerShell console, you may use $env: to set these environment variables. Each of the environment variables Terraform will search for while attempting to connect to Azure is listed below.

You may just reference the properties instead of copying and pasting them since the output of az ad sp create-for-rbac was stored to the $sp variable before.

$subscriptionId = $env:ARM SUBSCRIPTION ID $sp.appId = $env:ARM CLIENT ID $sp.password = $env:ARM CLIENT SECRET $sp.tenant = $env:ARM TENANT ID

Terraform is ready to connect to Azure after you’ve established the environment variables.

Make a file for configuration.

Terraform configurations are stored in a single folder, which is commonly named after a module. Although you will not be developing a module in this lesson, you will follow the similar procedure.

Create a folder named TerraformTesting anywhere you choose on your PowerShell terminal, then go to that directory.

cd TerraformTesting mkdir TerraformTesting

Create the main.tf configuration file after that. This is where the Terraform configuration for building the Azure VM will be saved. This is what most people refer to as the “primary” configuration file. It comprises declarations for all of the infrastructure that the configuration will control, including provider and resource declarations.

Michael Levan and I have a wonderful chapter about Terraform in our No BS Azure and DevOps eBook if you’d want to learn more about the syntax (HCL).

Inside the Terraformtesting directory, create a new main.tf file that looks like this.

## <https://www.terraform.io/docs/providers/azurerm/index.html> provider “azurerm” { version = “=2.5.0” features {} } ## <https://www.terraform.io/docs/providers/azurerm/r/resource_group.html> resource “azurerm_resource_group” “rg” { name = “TerraformTesting” location = “eastus” } ## <https://www.terraform.io/docs/providers/azurerm/r/availability_set.html> resource “azurerm_availability_set” “DemoAset” { name = “example-aset” location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } ## <https://www.terraform.io/docs/providers/azurerm/r/virtual_network.html> resource “azurerm_virtual_network” “vnet” { name = “vNet” address_space = [“10.0.0.0/16”] location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name } ## <https://www.terraform.io/docs/providers/azurerm/r/subnet.html> resource “azurerm_subnet” “subnet” { name = “internal” resource_group_name = azurerm_resource_group.rg.name virtual_network_name = azurerm_virtual_network.vnet.name address_prefix = “10.0.2.0/24” } ## <https://www.terraform.io/docs/providers/azurerm/r/network_interface.html> resource “azurerm_network_interface” “example” { name = “example-nic” location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_configuration { name = “internal” subnet_id = azurerm_subnet.subnet.id private_ip_address_allocation = “Dynamic” } } ## <https://www.terraform.io/docs/providers/azurerm/r/windows_virtual_machine.html> resource “azurerm_windows_virtual_machine” “example” { name = “example-machine” resource_group_name = azurerm_resource_group.rg.name location = azurerm_resource_group.rg.location size = “Standard_F2” admin_username = “adminuser” admin_password = “[email protected]$$w0rd1234!” availability_set_id = azurerm_availability_set.DemoAset.id network_interface_ids = [ azurerm_network_interface.example.id, ] os_disk { caching = “ReadWrite” storage_account_type = “Standard_LRS” } source_image_reference { publisher = “MicrosoftWindowsServer” offer = “WindowsServer” sku = “2016-Datacenter” version = “latest” } }

Terraform should be started.

When Terraform tries to build resources, it needs to know what sort of providers you’ll be utilizing. Because it downloads these providers in the same folder you’re working in, it must know this ahead of time.

Run terraform init to download the azurerm resource provider as described in the main configuration file. Once you’ve done that, you should see something like this.

Obtaining the azurermObtaining the azurerm

Confirm the configuration.

The configuration you created may not be perfect. I know, hard to believe, right? Terraform needs to Confirm the configuration. for any syntax errors in the configuration. To do that, run the terraform plan command. This command reads the configuration files in the directory and reports any errors.

Before trying to supply infrastructure, be careful to correct any flaws that the terraform plan may reveal!

Terraform is used to create the Azure VM.

Finally, by executing terraform apply, you can start building the Azure VM. Terraform reads any configuration files in the directory and asks you for confirmation when you run terraform apply. It will then contact Azure and begin constructing the VM and all related resources after you type “yes.”

Creating an Azure Virtual MachineCreating an Azure Virtual Machine

Complete the application if you see the bright and brilliant green! Terraform has successfully constructed the resources, according to the sentence at the bottom!

Clean Up

Because this was only an example and you’re probably not going to maintain this VM, be careful to undo all you’ve done.

For Terraform to authenticate to Azure, you defined a service principal. With the az ad sp delete command, you may get rid of it.

$spId = ((az ad sp list –all | ConvertFrom-Json) | Where-Object { ‘<http://TerraformTesting>’ -in $_.serviceprincipalnames }).objectId az ad sp delete –id $spId

Next, remove the Azure VM you just created and all other resources in the configuration file using terraform destroy. If you’d like to Confirm the configuration. and test what would happen if you were to run terraform destroy, you can also run terraform plan -destroy.

Conclusion

Terraform is a fantastic and free tool for creating infrastructure in a variety of fields. The most difficult aspect of Terraform is learning the syntax of HCL, however it is a very straightforward language. Learn Terraform if you’re contemplating about utilizing a tool like Terraform or ARM temples, for example.

Terraform is a well-known industry tool currently, with a thriving community and lots of individuals willing to assist!

Terraform is a tool that allows users to create, change, and destroy virtual machines. This guide will teach you how to use Terraform on Windows. Reference: terraform azure create vm from shared image gallery.

Frequently Asked Questions

How do I use terraform to make Azure VM?

A: How to use Terraform https://terraform.io/

How do I create a Windows Virtualform in terraform?

How do I set up an Azure VM?

A: To set up an Azure VM, you will need to register for at least a trial of Microsofts services. You can then start by creating your first virtual machine. The next step is putting the correct configurations into Windows Server 2008 R2 and installing SQL Server 2014 Express with Advanced Services on it.

Related Tags

  • how to create multiple vm in azure using terraform
  • terraform azure create vm with public ip
  • terraform azure vm size list
  • terraform azurerm_virtual_machine_extension windows
  • terraform azure vm module

Table of Content