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.
Before you begin, make sure you have the following:
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.
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
"name": "Microsoft Azure 赞助",
If you have multiple subscriptions, you need to set the subscription that your account uses.
az account set --subscription 00000000-0000-0000-0000-000000000000
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.
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:
Apply the code by running the following command in the resource-group directory:
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
Create a virtual machine
Navigate to the
virtual-machine directory and run the following command to initialize:
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:
Create the virtual machine by running the following command in the virtual-machine directory:
For example, if the vm_count is set to 3, the resources can be viewed by running the following command:
terraform state list
You can use the command
terraform show to view detailed information about resources, such as public IP addresses, and so on.