快速入门:使用 AzAPI Terraform 提供程序管理 Azure 密钥保管库 证书联系人

使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 HCL 语法允许你指定云提供商(如 Azure)和构成云基础结构的元素。 创建配置文件后,可以创建 一个执行计划 ,以便在部署基础结构更改之前预览这些更改。 验证更改后,应用执行计划来部署基础结构。

使用 azapi_data_plane_resource 管理 Azure 中的 Terraform 数据平面资源。 在此示例中,为Azure 密钥保管库配置证书联系人。

有关数据平面框架工作原理和 parent_id 模式的基本概念,请参阅 了解 AzAPI 数据平面框架

  • 使用 AzureRM 提供程序创建密钥保管库
  • 使用 azapi_data_plane_resource 配置证书联系人

先决条件

  • Azure 订阅:如果没有 Azure 订阅,请在开始之前创建 一个免费帐户

使用 Microsoft 帐户登录到 Azure 门户时,将使用该帐户的默认 Azure 订阅。

Terraform 使用默认 Azure 订阅中的信息自动进行身份验证。

运行 az account show 以验证当前Microsoft帐户和 Azure 订阅。

az account show

通过 Terraform 所做的任何更改都显示在显示的 Azure 订阅上。 如果这是你想要的,请跳过本文的其余部分。

实现 Terraform 代码

  1. 创建用于测试和运行示例 Terraform 代码的目录,并将其设为当前目录。

  2. 创建名为 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 {
        key_vault {
          purge_soft_delete_on_destroy    = true
          recover_soft_deleted_key_vaults = true
        }
      }
    }
    
    provider "azapi" {}
    
  3. 创建名为 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."
    }
    
  4. 创建名为 main.tf 的文件并插入下列代码:

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "random_string" "kv_suffix" {
      length  = 6
      upper   = false
      special = false
    }
    
    resource "azurerm_resource_group" "example" {
      location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    data "azurerm_client_config" "current" {}
    
    resource "azurerm_key_vault" "example" {
      name                = "kv-${random_string.kv_suffix.result}"
      location            = azurerm_resource_group.example.location
      resource_group_name = azurerm_resource_group.example.name
      tenant_id           = data.azurerm_client_config.current.tenant_id
      sku_name            = "standard"
    
      access_policy {
        tenant_id = data.azurerm_client_config.current.tenant_id
        object_id = data.azurerm_client_config.current.object_id
    
        certificate_permissions = [
          "ManageContacts",
        ]
      }
    }
    
    resource "azapi_data_plane_resource" "certificate_contacts" {
      type      = "Microsoft.KeyVault/vaults/certificates/contacts@7.3"
      parent_id = trimsuffix(trimprefix(azurerm_key_vault.example.vault_uri, "https://"), "/")
      name      = "default"
    
      body = {
        contacts = [
          {
            emailAddress = "admin@contoso.com"
            name         = "Admin Contact"
            phone        = "555-555-0100"
          },
          {
            emailAddress = "ops@contoso.com"
            name         = "Operations"
          }
        ]
      }
    }
    

    azapi_data_plane_resource 的要点:

    • type 字段使用数据平面 API 的格式 <resource-type>@<api-version>
    • parent_id数据平面终结点主机名(不含https://前缀),而不是 ARM 资源 ID。
    • 父级中的name字段用于识别特定资源。 对于 密钥保管库 证书联系人,值始终为 "default"。
  5. 创建名为 outputs.tf 的文件并插入下列代码:

    output "resource_group_name" {
      value = azurerm_resource_group.example.name
    }
    
    output "key_vault_name" {
      value = azurerm_key_vault.example.name
    }
    
    output "certificate_contacts" {
      value = azapi_data_plane_resource.certificate_contacts.output
    }
    

运行 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

验证结果

运行 az keyvault certificate contact list 以检索证书联系人。

```azurecli
az keyvault certificate contact list --vault-name <key_vault_name>
```

清理资源

如果不再需要通过 Terraform 创建的资源,请执行以下步骤:

  1. 运行 terraform plan 并指定 destroy 标志。

    terraform plan -destroy -out main.destroy.tfplan
    

    要点

    • terraform plan 命令创建执行计划,但不执行它。 相反,它会确定需要执行哪些操作,以创建配置文件中指定的配置。 此模式允许你在对实际资源进行任何更改之前验证执行计划是否符合预期。
    • 使用可选 -out 参数可以为计划指定输出文件。 使用 -out 参数可以确保所查看的计划与所应用的计划完全一致。
  2. 运行 terraform apply 来应用执行计划。

    terraform apply main.destroy.tfplan
    

解决 Azure 上的 Terraform 问题

排查在 Azure 上使用 Terraform 时遇到的常见问题

后续步骤

补充阅读