sudorpm--importhttps://packages.microsoft.com/keys/microsoft.ascsudodnfinstall-yhttps://packages.microsoft.com/config/rhel/8/packages-microsoft-prod.rpmsudodnfinstallazure-cli# 查看版本[root@centos8 azure-terraform]# az version{"azure-cli":"2.45.0","azure-cli-core":"2.45.0","azure-cli-telemetry":"1.0.8","extensions":{}}
使用Terraform创建资源组
先尝试实现一个最简单场景,了解下大概的使用流程。
创建配置文件
新建一个空目录,并在其中创建一个 main.tf 文件
mkdirazure-terraformcdazure-terraform/
编辑 main.tf 配置文件
[root@centos8 azure-terraform]# cat main.tf# We strongly recommend using the required_providers block to set the# Azure Provider source and version being usedterraform{required_providers{azurerm={source="hashicorp/azurerm"version="3.44.1" } }}# Configure the Microsoft Azure Providerprovider"azurerm"{features{}}# Create a resource groupresource"azurerm_resource_group""example"{name="example-resources"location="West Europe"}
[root@centos8 azure-terraform]# terraform planTerraformusedtheselectedproviderstogeneratethefollowingexecutionplan.Resourceactionsareindicatedwiththefollowingsymbols:+createTerraformwillperformthefollowingactions:# azurerm_resource_group.example will be created+resource"azurerm_resource_group""example"{+id= (known afterapply)+location="westeurope"+name="example-resources" }Plan:1toadd,0tochange,0todestroy.───────────────────────────────────────────────────────────────────────────────────────────────────────────Note:Youdidn't use the -out option to save this plan, so Terraform can'tguaranteetotakeexactlytheseactionsifyourun"terraform apply"now.
[root@centos8 azure-terraform]# terraform applyTerraformusedtheselectedproviderstogeneratethefollowingexecutionplan.Resourceactionsareindicatedwiththefollowingsymbols:+createTerraformwillperformthefollowingactions:# azurerm_resource_group.example will be created+resource"azurerm_resource_group""example"{+id= (known afterapply)+location="westeurope"+name="example-resources" }Plan:1toadd,0tochange,0todestroy.Doyouwanttoperformtheseactions?Terraformwillperformtheactionsdescribedabove.Only'yes'willbeacceptedtoapprove.Enteravalue:yesazurerm_resource_group.example:Creating...azurerm_resource_group.example: Creation complete after 6s [id=/subscriptions/3c398848-b31e-427a-aa4e-3b87b2ae6064/resourceGroups/example-resources]
Applycomplete!Resources:1added,0changed,0destroyed.
terraform show 查看当前部署
查看当前部署资源属性和元数据,可以用这些值来配置其他资源
[root@centos8 azure-terraform]# terraform show# azurerm_resource_group.example:resource"azurerm_resource_group""example"{id="/subscriptions/3c398848-b31e-427a-aa4e-3b87b2ae6064/resourceGroups/example-resources"location="westeurope"name="example-resources"}# terraform state list 命令可以列出创建的资源列表[root@centos8 azure-terraform]# terraform state listazurerm_resource_group.example
main.tf 增加vnet,sub,vm 等配置文件。注意下变量的引用,vnet 会引用RG名称,网卡引用subnet id
[root@centos8 azure-terraform]# cat main.tf# We strongly recommend using the required_providers block to set the# Azure Provider source and version being usedterraform{required_providers{azurerm={source="hashicorp/azurerm"version="3.44.1" } }}# Configure the Microsoft Azure Providerprovider"azurerm"{features{}}resource"azurerm_resource_group""main"{name="${var.prefix}-resources"location=var.location}resource"azurerm_virtual_network""main"{name="${var.prefix}-network"address_space= ["10.0.0.0/22"]location=azurerm_resource_group.main.locationresource_group_name=azurerm_resource_group.main.name}resource"azurerm_subnet""internal"{name="internal"resource_group_name=azurerm_resource_group.main.namevirtual_network_name=azurerm_virtual_network.main.nameaddress_prefixes= ["10.0.2.0/24"]}resource"azurerm_network_interface""main"{name="${var.prefix}-nic"resource_group_name=azurerm_resource_group.main.namelocation=azurerm_resource_group.main.locationip_configuration{name="internal"subnet_id=azurerm_subnet.internal.idprivate_ip_address_allocation="Dynamic" }}resource"azurerm_linux_virtual_machine""main"{name="${var.prefix}-vm"resource_group_name=azurerm_resource_group.main.namelocation=azurerm_resource_group.main.locationsize="Standard_D2s_v3"admin_username="${var.username}"admin_password="${var.password}"disable_password_authentication=falsenetwork_interface_ids= [azurerm_network_interface.main.id, ]source_image_reference{publisher="Canonical"offer="UbuntuServer"sku="18.04-LTS"version="latest" }os_disk{storage_account_type="Standard_LRS"caching="ReadWrite" }}
variables.tf 定义资源名称前缀,地区,vm的用户名密码
[root@centos8 azure-terraform]# cat variables.tfvariable"prefix"{description="The prefix which should be used for all resources in this example"type=stringdefault="demo"}variable"location"{description="The Azure Region in which all resources in this example should be created."type=stringdefault="eastus"}variable"username"{description="The virtual machine username."type=stringdefault="caifeng"}variable"password"{description="The virtual machine password."type=stringdefault="abcdef@123"}
terraform apply 更新变更,可以在portal 上查看RG,VM信息。 state list 查看资源列表。
[root@centos8 azure-terraform]# terraform apply[root@centos8 azure-terraform]# terraform state listazurerm_linux_virtual_machine.mainazurerm_network_interface.mainazurerm_resource_group.mainazurerm_subnet.internalazurerm_virtual_network.main