Create Azure virtual machines with Terraform

Addo Zhang
4 min readMar 2, 2023

I previously wrote an article about how to deploy Proxmox virtual machines using Terraform, which was done on a Homelab with a Core i7–8700 and 64 GB of RAM. This has been my testing environment ever since. However, last year I joined the Microsoft MVP program and received Azure credits. Since then, I have been using Azure virtual machines, especially in environments where I need a lot of resources or frequently pull images, such as in multi-cluster testing.

When creating virtual machines in Azure, I started with the web page and then moved to the CLI when complex configurations were needed. Nowadays, I mainly use Terraform for this purpose. This article will share how to use Terraform to configure the creation of Azure virtual machines.

Terraform is a software tool for infrastructure as code. Using Terraform, you can describe the desired state of your infrastructure (including cloud and local resources such as virtual machines, load balancers, and databases) by writing code. This allows you to build, modify, and version your cloud and local resources securely, flexibly, and efficiently.

Prerequisites

Before you begin, make sure you have the following:

Authentication

Since we want to automate the process, the first step is to solve the authentication problem. After all, it is not practical to authenticate manually every time, which does not match the characteristics of lazy guy.

The Azure Provider in Terraform supports five authentication methods. Since I only use it locally, this article will use Azure CLI for authentication.

Execute the following command to start authentication. Before authentication, you need to access https://portal.azure.com/#settings/directory to obtain your tenant information.

az login --tenant xxxx

After executing the command, you will be automatically redirected to your browser to complete the authentication process.

After successful authentication, you can use the following command to view your account information.

az account show
{
"environmentName": "AzureCloud",
"homeTenantId": "00000000-0000-0000-0000-000000000000",
"id": "00000000-0000-0000-0000-000000000000",
"isDefault": true,
"managedByTenants": [],
"name": "Microsoft Azure 赞助",
"state": "Enabled",
"tenantId": "00000000-0000-0000-0000-000000000000",
"user": {
"name": "mail@example.com",
"type": "user"
}
}

If you have multiple subscriptions, you need to set the subscription that your account uses.

az account set --subscription 00000000-0000-0000-0000-000000000000

Code

The script has been submitted on GitHub, and you can access https://github.com/addozhang/terraform-azure-sample to obtain it.

Clone the code to your local machine.

git clone https://github.com/addozhang/terraform-azure-sample.git

The script contains two parts:

  • resource-group: All resources on Azure are created under a resource group. Before creating a virtual machine, you need to create a resource group or use an existing one. Creating a resource group will also create a virtual network and subnet.
  • virtual-machine: As the name implies, it creates virtual machine resources.

Create a Resource Group

Go to the resource-group directory and run the following command to initialize Terraform and download the Azure provider.

terraform init

In the variables.tf file, input variables for resource group name and location are defined. The following command can be used to create a resource group, and parameters can be specified for the name and location during creation.

Check the Terraform code by running the following command:

terraform validate

Apply the code by running the following command in the resource-group directory:

terraform apply

Or, you can specify the name and location parameters during creation by using the following command:

terraform apply -var "name=demo" -var "location=eastasia"

View the created resources by running the command terraform state list. Alternatively, you can view the details of the resources by using the command terraform show.

terraform state list 
azurerm_resource_group.demo
azurerm_subnet.demo
azurerm_virtual_network.demo

Create a virtual machine

Navigate to the virtual-machine directory and run the following command to initialize:

terraform init

There are more parameters required to create a virtual machine, which are defined in variables.tf. The parameter values can be set in the terraform.tfvars file, and the subscription ID needs to be specified.

Check the code and parameters by running the following command:

terraform validate

Create the virtual machine by running the following command in the virtual-machine directory:

terraform apply

For example, if the vm_count is set to 3, the resources can be viewed by running the following command:

terraform state list
azurerm_linux_virtual_machine.demo[0]
azurerm_linux_virtual_machine.demo[1]
azurerm_linux_virtual_machine.demo[2]
azurerm_network_interface.demo[0]
azurerm_network_interface.demo[1]
azurerm_network_interface.demo[2]
azurerm_public_ip.demo[0]
azurerm_public_ip.demo[1]
azurerm_public_ip.demo[2]

You can use the command terraform show to view detailed information about resources, such as public IP addresses, and so on.

--

--

Addo Zhang

CNCF Ambassador | LF APAC OpenSource Evangelist | Microsoft MVP | SA and Evangelist at https://flomesh.io | Programmer | Blogger | Mazda Lover | Ex-BBer