使用 DM Convert 的最佳实践

转换为 Terraform 的最佳实践

状态

状态文件用于存储 Terraform 管理的资源的相关信息。默认情况下,Terraform 会将state存储在磁盘本地。如果您远程存储状态,则可以实现分布式协作、保护敏感信息,并在持续集成 (CI) 中运行 Terraform。

将 Deployment Manager 模板转换为 Terraform 并选择导入资源后,我们建议您按照相应步骤在 Cloud Storage 中远程存储状态

单元

如果您希望降低复杂性、强制执行一致性并提高配置的可重用性,则可以使用 Terraform 模块来封装资源集合。

如需使用模块,您可以执行以下任一操作:

对于大多数用例,我们建议您使用已发布的模块

创建自定义模块

  1. 转换配置后,确定要将哪些资源移到模块中。

  2. 将这些资源的配置移到模块目录中,并将所需变量转换为参数。

    以下示例展示了如何将 google_bigquery_datasetgoogle_bigquery_table 移入模块:

    # bq-module/main.tf
    resource "google_bigquery_dataset" "bigquerydataset" {
      provider = google-beta
    
      default_table_expiration_ms = 36000000
      location = "us-west1"
      dataset_id = var.dataset_id
      project = var.project_id
    }
    
    resource "google_bigquery_table" "bigquerytable" {
      provider = google-beta
    
      labels = {
        data-source = "external"
        schema-type = "auto-junk"
      }
      dataset_id = var.dataset_id
      project = var.project_id
      table_id = var.table_id
    
      depends_on = [
        google_bigquery_dataset.bigquerydataset
      ]
    }
    
    # bq-module/variables.tf
    variable "project_id" {
      description = "Project ID"
      type = string
    }
    
    variable "dataset_id" {
      description = "Dataset ID"
      type = string
    }
    
    variable "table_id" {
      description = "Table ID"
      type = string
    }
    
  3. 在导出的 main.tf 文件中,将原始配置替换为您创建的模块。

    以下示例使用上一步的示例中创建的模块展示此替换操作。

    # main.tf
    module "bq" {
      source = "./bq-module"
    
      project_id = "PROJECT_ID"
      dataset_id = "bigquerydataset"
      table_id   = "bigquerytable"
    }
    
  4. 如需初始化本地模块,请运行以下命令:

    terraform init
    
  5. 将与资源关联的 Terraform 状态移动到模块实例中。

    如需移动上一步中的示例模块,请运行以下命令:

    terraform state mv google_bigquery_dataset.bigquerydataset module.bq.google_bigquery_dataset.bigquerydataset
    terraform state mv google_bigquery_table.bigquerytable module.bq.google_bigquery_table.bigquerytable
    

    在此示例中,此移动的输出结果为:

    Move "google_bigquery_dataset.bigquerydataset" to "module.bq.google_bigquery_dataset.bigquerydataset"
    Successfully moved 1 object(s).
    Move "google_bigquery_table.bigquerytable" to "module.bq.google_bigquery_table.bigquerytable"
    Successfully moved 1 object(s).
    
  6. 运行以下命令,验证资源是否未发生更改:

    terraform plan
    

    以下是运行该命令后收到的输出示例:

    No changes. Your infrastructure matches the configuration.
    

使用已发布的模块

  1. 转换配置后,确定已发布的模块以及要移至该模块中的资源。

  2. 阅读模块文档,确定该模块的配置选项。

  3. 创建根据当前资源配置的模块实例。

    例如,如果您要将 google_bigquery_datasetgoogle_bigquery_table 移入官方 BigQuery 模块,该模块可能如下所示:

    module "bq" {
      source  = "terraform-google-modules/bigquery/google"
      version = "~> 5.0"
    
      project_id          = "PROJECT_ID"
      dataset_id          = "bigquerydataset"
      location            = "us-west1"
      deletion_protection = true
    
      tables = [
        {
          table_id           = "bigquerytable",
          friendly_name      = "bigquerytable"
          time_partitioning  = null,
          range_partitioning = null,
          expiration_time    = null,
          clustering         = [],
          schema             = null,
          labels = {
            data-source = "external"
            schema-type = "auto-junk"
          },
        }
      ]
    }
    
  4. 如需初始化本地模块,请运行以下命令:

    terraform init
    
  5. 阅读模块源代码,以识别上游模块中的资源地址并构建 move 命令。

    terraform state mv google_bigquery_dataset.bigquerydataset  module.bq.google_bigquery_dataset.main
    terraform state mv google_bigquery_table.bigquerytable 'module.bq.google_bigquery_table.main["bigquerytable"]'
    
  6. 如需查看配置的所有更改,请运行以下命令:

    terraform plan
    

如果您选择的已发布模块具有不同的默认设置或配置与您的配置不同,则可能会发现运行该命令的输出中突出显示了差异。

执行

我们建议您使用持续集成 (CI) 系统(例如 Cloud Build、Jenkins 或 GitHub Actions)来大规模自动运行 Terraform。如需了解详情,请参阅使用 Terraform、Cloud Build 和 GitOps 以代码形式管理基础架构

如果您希望引导创建触发器并简化身份验证,则可以选择使用 Cloud Build 工作区蓝图。

结构

从 DM Convert 转换的每个配置都是映射到单个状态文件的单个根配置。我们建议不要设置一个状态文件来保存大量资源。转换配置后,建议您确保新配置遵循根模块最佳实践