Azure 上的 Terraform
配置 AzureRM Terraform 提供程序,编写基本基础结构计划,并了解何时应优先使用 Terraform 而不是原生 ARM/Bicep 工具。
Azure 上的 Terraform 是 CoddyKit 上的免费 Azure Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Azure Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Azure Fundamentals 课程共包含 4 节课。
Terraform 是什么,以及为什么选择 Azure
Terraform 是由 HashiCorp 开发的开源基础结构即代码工具。与 Bicep 不同,Terraform 与云平台无关:单个 Terraform 代码库可以同时管理 Azure、AWS、GCP 以及许多其他提供程序中的资源。在 Azure 上,Terraform 使用 AzureRM 提供程序与 ARM API 交互,因此成为多云组织的热门选择。
AzureRM Terraform 提供程序
AzureRM 提供程序是 Terraform 下载并使用的插件,用于将 Terraform 配置转换为 ARM API 调用。它由 HashiCorp 和 Microsoft 维护,是使用 Terraform 管理 Azure 资源的官方方式。请使用您的订阅 ID 和身份验证方法配置提供程序,然后运行 terraform init 进行下载。
# main.tf — configure the AzureRM provider
terraform {
required_providers {
azurerm = {
source = 'hashicorp/azurerm'
version = '~> 3.0'
}
}
}
provider 'azurerm' {
features {}
subscription_id = var.subscription_id
}Terraform 文件结构
典型的 Azure Terraform 项目会拆分到多个 .tf 文件中:main.tf 保存资源声明,variables.tf 定义输入变量,outputs.tf 定义输出值,terraform.tfvars 为特定环境提供变量值。Terraform 会自动加载当前目录中的所有 .tf 文件,因此拆分为多个文件完全是为了提高可读性。
# variables.tf
variable 'resource_group_name' {
description = 'Name of the resource group'
type = string
default = 'my-rg'
}
variable 'location' {
description = 'Azure region'
type = string
default = 'eastus'
}声明 Azure 资源
在 Terraform 中,您可以使用 resource 块声明 Azure 资源。块类型为 resource,后面跟随 AzureRM 资源类型(例如 'azurerm_resource_group')以及用于在配置中引用该资源的本地名称。Terraform 中的属性名称使用 snake_case,而 ARM JSON 使用 camelCase 或 PascalCase。
# main.tf — create a resource group and storage account
resource 'azurerm_resource_group' 'main' {
name = var.resource_group_name
location = var.location
}
resource 'azurerm_storage_account' 'storage' {
name = 'mystorageaccount'
resource_group_name = azurerm_resource_group.main.name
location = azurerm_resource_group.main.location
account_tier = 'Standard'
account_replication_type = 'LRS'
tags = {
environment = 'dev'
}
}Terraform 工作流:Init、Plan、Apply
Terraform 的核心工作流包含三个步骤。terraform init 下载 AzureRM 提供程序并设置后端。terraform plan 将您的配置与当前状态进行比较,并显示将要创建、更改或销毁的内容差异,类似于 ARM 的 what-if。terraform apply 执行该计划,并更新 Azure 以匹配您的配置。
# Step 1: Initialise the project and download providers
terraform init
# Step 2: Preview changes (shows + create, ~ update, - destroy)
terraform plan -out=tfplan
# Step 3: Apply the plan
terraform apply tfplan
# Destroy all managed resources
terraform destroyTerraform 状态
Terraform 会维护一个状态文件(terraform.tfstate),用于记录所有受管理资源的当前状态,并将这些资源映射到您的配置。此文件使 Terraform 能够检测配置内容与 Azure 中实际存在内容之间的偏差。在团队环境中,请将状态远程存储在Azure Storage Account(Terraform 后端)中,以便所有团队成员共享同一个状态文件。
# Configure remote state in Azure Blob Storage
terraform {
backend 'azurerm' {
resource_group_name = 'tfstate-rg'
storage_account_name = 'tfstateaccount'
container_name = 'tfstate'
key = 'prod.terraform.tfstate'
}
}Terraform 在 Azure 中的身份验证
Terraform 支持多种 Azure 身份验证方法。在本地开发期间,请使用 az login(Azure CLI 身份验证),Terraform 会自动获取凭据。在 CI/CD 流水线中,请使用带有客户端机密或证书的服务主体,并通过环境变量传入。在 Azure 托管的代理或虚拟机上,请使用托管标识,以实现最安全且无需机密的身份验证。
# Option 1: Azure CLI (local dev)
az login
terraform plan
# Option 2: Service principal via environment variables
export ARM_CLIENT_ID='00000000-0000-0000-0000-000000000000'
export ARM_CLIENT_SECRET='your-client-secret'
export ARM_SUBSCRIPTION_ID='00000000-0000-0000-0000-000000000000'
export ARM_TENANT_ID='00000000-0000-0000-0000-000000000000'
terraform plan用于重复使用的 Terraform 模块
与 Bicep 模块一样,Terraform 模块可以打包并重复使用基础结构模式。模块是一个包含 .tf 文件的目录。您可以使用 module 块引用模块,通过 variables 传递输入,并使用其输出。Terraform 注册表托管了数百个由社区提供并经过验证的 Azure 模块,您可以直接使用,例如 AzureRM 网络模块。
# Use a module from the Terraform Registry
module 'vnet' {
source = 'Azure/vnet/azurerm'
version = '4.0.0'
resource_group_name = azurerm_resource_group.main.name
vnet_location = azurerm_resource_group.main.location
use_for_each = true
address_space = ['10.0.0.0/16']
subnet_prefixes = ['10.0.1.0/24', '10.0.2.0/24']
subnet_names = ['web', 'app']
}何时选择 Terraform 而不是 Bicep
如果您的组织只使用 Azure,并希望与 Azure 新功能实现最紧密的集成,请选择 Bicep(Bicep 在新资源类型发布当天就能获得支持)。如果您要管理多个云提供商中的资源,需要丰富的社区模块生态,或者偏好 HCL 语法和 HashiCorp 工具链,请选择 Terraform。对于完全基于 Azure 的部署,两者都适用,通常由组织偏好决定。
导入现有资源
如果您开始使用 Terraform 之前 Azure 中已经存在资源,可以使用 terraform import 将这些资源纳入 Terraform 管理。此命令会获取资源的当前状态,并将其添加到 Terraform 状态文件中。导入后,您必须手动编写相应的配置块,使其与导入的资源匹配,然后运行 terraform plan,确认不存在差异。
# Import an existing resource group into Terraform state
terraform import \
azurerm_resource_group.main \
'/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/existing-rg'
# Terraform 1.5+ supports import blocks in configuration
import {
to = azurerm_resource_group.main
id = '/subscriptions/00000000.../resourceGroups/existing-rg'
}Azure 上的 Terraform 最佳实践
请遵循以下 Azure 上使用 Terraform 的最佳实践:在启用状态锁定的情况下将状态远程存储在 Azure Blob Storage 中;针对不同环境使用工作区或单独的状态文件;绝不要将机密提交到源代码管理中,而应使用 Azure Key Vault 数据源;固定提供程序版本以避免破坏性更改;并在 CI/CD 流水线(Azure Pipelines 或 GitHub Actions)中运行 Terraform,同时设置计划审批门禁。
快速检查
测试您对本课 Microsoft Azure 基础知识(AZ-900)概念的理解。
课程回顾
在本课中,您学到了:Terraform 是一种与云平台无关的 IaC 工具,通过 AzureRM 提供程序管理 Azure 资源;init、plan、apply 工作流支持安全且可预测的部署;Azure Blob Storage 中的远程状态对于团队协作至关重要。接下来我们将学习创建 App Service 计划和 Web 应用。
常见问题解答
「Azure 上的 Terraform」课时是免费的吗?
是的 — 「Azure 上的 Terraform」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Azure Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Azure Fundamentals 课程共包含 4 节课。
「Azure 上的 Terraform」这节课中我会学到什么?
配置 AzureRM Terraform 提供程序,编写基本基础结构计划,并了解何时应优先使用 Terraform 而不是原生 ARM/Bicep 工具。 你通过在浏览器中直接运行的动手代码来练习 Azure Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Azure Fundamentals 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Azure Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「Azure 上的 Terraform」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Azure Fundamentals 课中编写并运行代码吗?
能。每节 Azure Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Azure 资源管理器的工作原理
- 编写 ARM 模板
- Bicep:现代 Azure IaC
- Azure 上的 Terraform