Google 带有 Beta 参数的云平台资源的正确 Terraform 提供程序配置是什么?

What is the Correct Terraform Provider Configuration for Google Cloud Platform Resources with Beta Arguments?

地形化由任何 beta 参数定义的任何 Google 云平台 (GCP) 资源需要 google-beta 提供程序。是否应该使用 google-beta 提供程序而不是 google 提供程序一起使用?

换句话说,假设某个 Google Kubernetes Engine (GKE) 集群 $GKE_CLUSTER_NAME 存在于 GCP 项目中 $GCP_PROJECT_NAME:

gcloud container clusters list \
--format="value(name)" \
--project=$GCP_PROJECT_NAME

#=>

. . .
$GKE_CLUSTER_NAME
. . .

启用配置连接器:

gcloud container clusters describe $GKE_CLUSTER_NAME \
--format=“value(addonsConfig.configConnectorConfig.enabled)” \
--zone=$GKE_CLUSTER_ZONE

#=>

True

Terraforming $GKE_CLUSTER_NAME 需要 container_cluster.tf 中的 google_container_cluster 资源定义,其中包括 config_connector_config 参数(在 addons_config 块中;更多 here) 和 provider 参数(官方参考文档中缺失):

resource "google_container_cluster" "test" {
  addons_config {
    config_connector_config {
      enabled = true
    }
    . . .
  }
  . . .
  provider        = google-beta
  . . .
}

需要google-beta provider定义providers.tf:

provider "google" {
  project = ". . ."
}

terraform {
  required_providers {
    google = {
      version = "~> 3.83.0"
    }
  }
}

这和其他资源定义中缺少 provider 参数,例如 container_node_pool.tf 中的 google_container_node_pool,导致 providers 的以下输出命令:

terraform providers

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/google] ~> 3.83.0
└── provider[registry.terraform.io/hashicorp/google-beta]

Providers required by state:

    provider[registry.terraform.io/hashicorp/google]

    provider[registry.terraform.io/hashicorp/google-beta]

apply 命令刷新 terraform.tfstate 状态文件后。

使用 beta 参数对 GCP 资源进行 Terraforming GCP 资源的方法是否更正确且不易出错?或者,我应该 运行 一个 replace-provider 子命令来代替:

terraform state replace-provider \
-auto-approve \
"hashicorp/google" \
"hashicorp/google-beta"

#=>

Terraform will perform the following actions:

  ~ Updating provider:
    - registry.terraform.io/hashicorp/google
    + registry.terraform.io/hashicorp/google-beta

Changing 2 resources:

  google_container_node_pool.$GKE_NODE_POOL_NAME

Successfully replaced provider for 1 resources.

并修改providers.tf:

provider "google-beta" {
  project = ". . ."
}

terraform {
  required_providers {
    google-beta = {
      version = "~> 3.83.0"
    }
  }
}

所以providers命令的输出是:

terraform providers

#=>

Providers required by configuration:
.
└── provider[registry.terraform.io/hashicorp/google-beta] ~> 3.83.0

Providers required by state:

    provider[registry.terraform.io/hashicorp/google-beta]

apply 命令刷新 terraform.state 中的状态之后?

应该同时使用googlegoogle-beta提供商。

在同一个 providers.tf 中同时使用 googlegoogle-beta 提供商是 安全的 。 Terraform 将对需要 google-beta 提供程序的任何资源的请求发送到 Beta 端点:https://. . .googleapis.com/v1beta1/. . .;即,使用 google-beta 提供程序类似于使用 beta gcloud 组。

应该:

  • providers.tf 中包含 googlegoogle-beta 提供程序:

    provider "google" {
      project = ". . ."
    }
    
    provider "google-beta" {
      project = ". . ."
    }
    
    terraform {
      required_providers {
        google = {
          version = "~> 3.83.0"
        }
        google-beta = {
          version = "~> 3.83.0"
        }
      }
    }
    
  • 每个 GCP 资源使用provider 参数:google-beta 对具有 至少一项已启用的 Beta 功能:

    resource "google_container_cluster" "beta_cluster" {
       . . .
       provider        = google-beta
       . . .
    }
    

    google 对于所有其他资源:

    resource "google_container_node_pool" "general_availability_node_pool" {
    . . .
      provider       = google
    . . .
    }
    

在对上述建议的更改进行 boththen 运行 a refresh 后,输出 providers 命令现在应该类似于:

terraform providers

#=>

Providers required by configuration:
.
├── provider[registry.terraform.io/hashicorp/google] ~> 3.83.0
└── provider[registry.terraform.io/hashicorp/google-beta] ~> 3.83.0

Providers required by state:

    provider[registry.terraform.io/hashicorp/google]

    provider[registry.terraform.io/hashicorp/google-beta]

应该阅读官方文档。对于提供者版本 here.