使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 HCL 语法允许你指定云提供商(如 Azure)和构成云基础结构的元素。 创建配置文件后,可以创建 一个执行计划 ,以便在部署基础结构更改之前预览这些更改。 验证更改后,应用执行计划来部署基础结构。
使用 azapi_resource_action 作为托管的 Terraform 资源,对 Azure 资源执行必须的状态更改操作。 在此示例中,你将创建一个Azure存储帐户,然后轮换其访问密钥。
azapi_resource_action 有两种使用形式:
- 资源:在 过程期间执行状态更改操作。 Terraform 跟踪状态中的操作,并且可以选择性地将其反转
terraform destroy。 - 数据源:在规划期间执行只读操作。 请参阅 资源操作数据源快速入门 以获取该场景的信息。
如果需要 Terraform 执行一个不属于标准的创建/读取/更新/删除生命周期的 Azure 操作(例如轮换凭据、启动或停止 VM 或触发故障转移),请使用资源表单。
- 使用 AzureRM 提供程序创建存储帐户
- 使用
azapi_resource_action轮换存储帐户访问密钥
先决条件
- Azure 订阅:如果没有 Azure 订阅,请在开始之前创建 一个免费帐户 。
配置 Terraform:如果尚未这样做,请使用以下选项之一配置 Terraform:
- 使用 Bash 在 Azure Cloud Shell 中配置 Terraform
- 使用 PowerShell 在 Azure Cloud Shell 中配置 Terraform
- 在 Windows 中使用 Bash 配置 Terraform
- 使用 PowerShell 在 Windows 中配置 Terraform
使用 Microsoft 帐户登录到 Azure 门户时,将使用该帐户的默认 Azure 订阅。
Terraform 使用默认 Azure 订阅中的信息自动进行身份验证。
运行 az account show 以验证当前Microsoft帐户和 Azure 订阅。
az account show
通过 Terraform 所做的任何更改都显示在显示的 Azure 订阅上。 如果这是你想要的,请跳过本文的其余部分。
实现 Terraform 代码
创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。
创建名为
providers.tf的文件并插入下列代码:terraform { required_providers { azapi = { source = "Azure/azapi" version = "~> 2.0" } azurerm = { source = "hashicorp/azurerm" version = "~> 4.0" } random = { source = "hashicorp/random" version = "~> 3.0" } } } provider "azurerm" { features {} } provider "azapi" {}创建名为
variables.tf的文件并插入下列代码:variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random value to create a unique name." } variable "storage_account_name_prefix" { type = string default = "st" description = "Prefix of the storage account name that's combined with a random value to create a unique name." }创建名为
main.tf的文件并插入下列代码:resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } resource "random_string" "storage_suffix" { length = 8 upper = false special = false } resource "azurerm_resource_group" "example" { location = var.resource_group_location name = random_pet.rg_name.id } resource "azurerm_storage_account" "example" { name = "${var.storage_account_name_prefix}${random_string.storage_suffix.result}" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = "Standard" account_replication_type = "LRS" } resource "azapi_resource_action" "regenerate_key" { type = "Microsoft.Storage/storageAccounts@2023-01-01" resource_id = azurerm_storage_account.example.id action = "regenerateKey" method = "POST" body = { keyName = "key1" } }有关用作
azapi_resource_action资源的要点:- 该
action字段指定要执行的 ARM 操作。 对于存储帐户密钥轮换,请使用regenerateKey。 - 该
method字段指定 HTTP 方法。 大多数命令性操作都使用POST。 - 该
body属性将数据传递给操作。 对于密钥重新生成,请指定要旋转的键(key1或key2)。 - 操作在
terraform apply期间执行,并在 Terraform 状态中跟踪。
- 该
创建名为
outputs.tf的文件并插入下列代码:output "resource_group_name" { value = azurerm_resource_group.example.name } output "storage_account_name" { value = azurerm_storage_account.example.name }
运行 terraform init,将 Terraform 部署进行初始化。 此命令下载 Azure 提供程序,以便管理您的 Azure 资源。
terraform init -upgrade
要点:
- 参数
-upgrade可将必要的提供程序插件升级到符合配置版本约束的最新版本。
运行 terraform plan 以创建执行计划。
terraform plan -out main.tfplan
要点:
-
terraform plan命令创建执行计划,但不执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。 - 使用可选
-out参数可以为计划指定输出文件。 使用-out参数可以确保所查看的计划与所应用的计划完全一致。
运行 terraform apply 以将执行计划应用到您的云基础架构。
terraform apply main.tfplan
要点:
- 示例
terraform apply命令假设你先前运行了terraform plan -out main.tfplan。 - 如果为
-out参数指定了不同的文件名,请在对terraform apply的调用中使用该相同文件名。 - 如果未使用参数
-out,则无需任何参数即可调用terraform apply。
验证结果
terraform apply完成后,存储帐户密钥已轮换。 可以通过检查Azure中的存储帐户密钥来验证密钥轮换。
运行 az storage account keys list 以查看存储帐户密钥。
az storage account keys list \
--resource-group <resource_group_name> \
--account-name <storage_account_name>
该 value 字段显示当前键。
其他资源操作的示例
azapi_resource_action 资源适用于许多Azure操作。 下面是常见示例:
-
虚拟机:
deallocate、start、restart、powerOff、reimage -
密钥保管库:
purge(对于软删除的保管库)、rotate(对于托管密钥) -
应用服务:
swap(对于部署插槽),restart -
数据库:
failoverpromote - Compute 资源:Azure REST API 公开的任何操作,无需创建或销毁资源即可修改状态
清理资源
如果不再需要通过 Terraform 创建的资源,请执行以下步骤:
运行 terraform plan 并指定
destroy标志。terraform plan -destroy -out main.destroy.tfplan要点:
-
terraform plan命令创建执行计划,但不执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。 - 使用可选
-out参数可以为计划指定输出文件。 使用-out参数可以确保所查看的计划与所应用的计划完全一致。
-
运行 terraform apply 来应用执行计划。
terraform apply main.destroy.tfplan
解决 Azure 上的 Terraform 问题
排查在 Azure 上使用 Terraform 时遇到的常见问题