I have previously written an article about using Terraform to deploy a Proxmox virtual machine . It is a virtual machine environment built on a Core i7-8700 + 64G Homelab. This is also my experimental environment. Until I joined Microsoft MVP last year and got Azure credit, I would also choose to use Azure virtual machines when I needed more resources or pulled images frequently, especially recently when I often do testing in a multi-cluster environment.
When creating virtual machines on Azure, I started with a web page-specific CLI, and now use Terraform when complex configuration is required. This article will share how to use Terraform to configure the creation of Azure virtual machines.
Terraform is an infrastructure-as-code software tool. Using Terraform, you can write code to describe the desired state of the infrastructure (including cloud and local resources such as virtual machines, load balancers, databases, etc.), and build, change, and version cloud and local resources safely, flexibly, and efficiently.
Preconditions
Before starting, make sure you have the following conditions:
- Azure account
- Terraform CLI
- Azure CLI
Certification
Since we want to automate, the first step is to solve the authentication problem. After all, it is unrealistic to perform manual authentication every time, and it is not in line with the characteristics of lazy people.
Terraform's Azure Provider supports 5 authentication methods . Since I only use it locally, this article is authenticated through Azure CLI .
Execute the following command to enable authentication. Before authentication, you need to visit https://portal.azure.com/#settings/directory to obtain tenant information.
az login --tenant xxxx
After executing the command, you will automatically jump to the browser to complete the authentication process.
After successful authentication, account information can be viewed through commands.
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": "[email protected]",
"type": "user"
}
}
If there are multiple subscriptions, you need to set the subscription used by the current account.
az account set --subscription 00000000-0000-0000-0000-000000000000
code
The script has been submitted on GitHub and can be obtained at https://github.com/addozhang/terraform-azure-sample .
Clone the code locally.
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 certain resource group. Before creating a virtual machine, you need to create a resource group or use an existing resource group. When you create a resource group, virtual networks and subnets are also created.virtual-machine
: As the name suggests, create virtual machine resources.
Create resource group
Enter the directory resource-group
and execute the following commands to initialize Terraform and download the Azure provider.
terraform init
variables.tf
The input parameters are defined in the file: resource group name and location . Execute the following command to create a resource group, or specify the name and location through parameters when creating it.
Execute the command to inspect the Terraform code.
terraform validate
Execute the following command to apply the code.
# 在 resource-group 目录中执行
terraform apply
# 或者
terraform apply -var "name=demo" -var "location=eastasia"
By terraform state list
viewing the resource created. Or terrafor show
view resource details via .
terraform state list
azurerm_resource_group.demo
azurerm_subnet.demo
azurerm_virtual_network.demo
Create a virtual machine
Enter the directory virtual-machine
directory and still run the command first to initialize.
terraform init
There will be more parameters to create a virtual machine. The specific parameter definitions can be viewed . Parameter values can be set variables.tf
through the file. Here you need to specify the subscription ID used .terraform.tfvars
Perform code and parameter checks.
terraform validate
Execute the command to create the virtual machine.
# 在 virtual-machine 目录中执行
terraform apply
For example, set the number of virtual machines vm_count
to 3
and view the resources after executing the above 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]
Through the command, terraform show
you can view the detailed information of the resource, such as the IP address of the public network, etc.
The web version of Windows 12 deepin-IDE compiled by junior high school students was officially unveiled. It is known as "truly independently developed" QQ has achieved "three-terminal simultaneous updates", and the underlying NT architecture is based on Electron QQ for Linux officially released 3.2.0 "Father of Hongmeng" Wang Chenglu : Hongmeng PC version system will be launched next year to challenge ChatGPT, these 8 domestic AI large model products GitUI v0.24.0 are released, the default wallpaper of Ubuntu 23.10, a Git terminal written in Rust, is revealed, the "Tauren" in the maze JetBrains announces the WebStorm 2023.3 roadmap China Human Java Ecosystem, Solon v2.5.3 releasedArticles are published uniformly on the public account
云原生指北