你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

快速入门:使用 Terraform 创建 Azure 机密账本

在本快速入门中,我们使用 Terraform 创建 Azure 资源组和机密账本。 Azure 机密账本是一种完全托管的服务,它提供用于存储敏感数据的防篡改登记簿。 它建立在 Azure 机密计算之上,后者使用基于硬件的受信任执行环境来保护数据免受威胁,即使在数据处于使用状态时也是如此。 该账本旨在维护其存储的数据的机密性和完整性,因此是需要高级别数据保护的用例的理想选择。 此脚本中创建的资源包括 Azure 机密账本和 Azure 资源组。

使用 Terraform 可以定义、预览和部署云基础结构。 使用 Terraform 时,请使用 HCL 语法来创建配置文件。 利用 HCL 语法,可指定 Azure 这样的云提供程序和构成云基础结构的元素。 创建配置文件后,请创建一个执行计划,利用该计划,可在部署基础结构更改之前先预览这些更改。 验证了更改后,请应用该执行计划以部署基础结构。

  • 为资源组生成随机宠物名称。
  • 使用生成的名称创建 Azure 资源组。
  • 检索当前的 Azure 客户端配置。
  • 为 Azure 机密账本名称生成随机字符串。
  • 使用生成的名称创建一个 Azure 机密账本,并将其分配给资源组。
  • 将基于 Azure AD 的服务主体分配给机密账本。
  • 将机密账本标记为示例。
  • 输出资源组名称、机密账本名称、机密账本类型和机密账本角色名称。
  • 指定所需的 Terraform 版本和所需的提供程序。
  • 定义没有其他功能的 Azure 提供程序。
  • 为资源组名称前缀、资源组位置、机密账本名称、机密账本类型和机密账本角色名称定义变量。

先决条件

实现 Terraform 代码

注意

本文中的示例代码位于 Azure Terraform GitHub 存储库中。 你可以查看包含当前和以前 Terraform 版本的测试结果的日志文件。

请参阅更多演示如何使用 Terraform 管理 Azure 资源的文章和示例代码

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

  2. 创建名为 main.tf 的文件并插入以下代码:

    resource "random_pet" "rg_name" {
      prefix = var.resource_group_name_prefix
    }
    
    resource "azurerm_resource_group" "rg" {
      ___location = var.resource_group_location
      name     = random_pet.rg_name.id
    }
    
    data "azurerm_client_config" "current" {
    }
    
    resource "random_string" "azurerm_confidential_ledger_name" {
      length  = 13
      lower   = true
      numeric = false
      special = false
      upper   = false
    }
    
    resource "azurerm_confidential_ledger" "example" {
      name                = coalesce(var.confidential_ledger_name, "ledger-${random_string.azurerm_confidential_ledger_name.result}")
      resource_group_name = azurerm_resource_group.rg.name
      ___location            = azurerm_resource_group.rg.___location
      ledger_type         = var.confidential_ledger_type
    
      azuread_based_service_principal {
        principal_id     = data.azurerm_client_config.current.object_id
        tenant_id        = data.azurerm_client_config.current.tenant_id
        ledger_role_name = var.confidential_ledger_role_name
      }
    
      tags = {
        IsExample = "True"
      }
    }
    
  3. 创建名为 outputs.tf 的文件并插入以下代码:

    output "resource_group_name" {
      value = azurerm_resource_group.rg.name
    }
    
    output "confidential_ledger_name" {
      value = azurerm_confidential_ledger.example.name
    }
    
    output "confidential_ledger_type" {
      value = azurerm_confidential_ledger.example.ledger_type
    }
    
    output "confidential_ledger_role_name" {
      value = azurerm_confidential_ledger.example.azuread_based_service_principal[0].ledger_role_name
    }
    
  4. 创建名为 providers.tf 的文件并插入以下代码:

    terraform {
      required_version = ">=1.0"
    
      required_providers {
        azurerm = {
          source  = "hashicorp/azurerm"
          version = "~>3.0"
        }
        random = {
          source  = "hashicorp/random"
          version = "~>3.0"
        }
      }
    }
    
    provider "azurerm" {
      features {}
    }
    
  5. 创建名为 variables.tf 的文件并插入以下代码:

    variable "resource_group_name_prefix" {
      type        = string
      default     = "rg"
      description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription."
    }
    
    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "confidential_ledger_name" {
      type        = string
      description = "The name of the confidential ledger resource. The value will be randomly generated if blank."
      default     = ""
    }
    
    variable "confidential_ledger_type" {
      type        = string
      default     = "Public"
      validation {
        condition     = contains(["Public", "Private"], var.confidential_ledger_type)
        error_message = "The confidential ledger type value must be one of the following: Public, Private."
      }
      description = "Type of the confidential ledger."
    }
    
    variable "confidential_ledger_role_name" {
      type        = string
      default     = "Administrator"
      description = "Role name for the confidential ledger."
    }
    

初始化 Terraform

运行 terraform init,将 Terraform 部署进行初始化。 此命令将下载管理 Azure 资源所需的 Azure 提供程序。

terraform init -upgrade

要点:

  • 参数 -upgrade 可将必要的提供程序插件升级到符合配置版本约束的最新版本。

创建 Terraform 执行计划

运行 terraform plan 以创建执行计划。

terraform plan -out main.tfplan

要点:

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

应用 Terraform 执行计划

运行 terraform apply,将执行计划应用到云基础结构。

terraform apply main.tfplan

要点:

  • 示例 terraform apply 命令假设你先前运行了 terraform plan -out main.tfplan
  • 如果为 -out 参数指定了不同的文件名,请在对 terraform apply 的调用中使用该相同文件名。
  • 如果未使用 -out 参数,请调用不带任何参数的 terraform apply

验证结果

Azure 命令行接口 (CLI)

运行 az confidential-ledger show 以查看 Azure 机密账本。

az confidentialledger show --ledger-name <ledger_name> --resource-group <resource_group_name>

必须将 <ledger_name> 替换为 Azure 机密账本的名称,将 <resource_group_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 时遇到的常见问题

后续步骤