具有 Beta 参数的 Google Cloud Platform 资源的正确 Terraform 提供程序配置是什么? [英] What is the Correct Terraform Provider Configuration for Google Cloud Platform Resources with Beta Arguments?

本文介绍了具有 Beta 参数的 Google Cloud Platform 资源的正确 Terraform 提供程序配置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

改造任何由任何 beta 参数定义的 Google Cloud Platform (GCP) 资源都需要 google-beta 提供程序.应该使用 google-beta 提供程序而不是 google 提供程序一起使用吗?

Terraforming any Google Cloud Platform (GCP) resource defined by any beta arguments requires the google-beta provider. Should the google-beta provider be used instead of or in tandem with the google provider?

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

In other words, say a certain Google Kubernetes Engine (GKE) cluster $GKE_CLUSTER_NAME exists within a GCP project $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 参数(官方参考文档中缺少):

Terraforming $GKE_CLUSTER_NAME requires a google_container_cluster resource definition within container_cluster.tf that includes both the config_connector_config argument (within the addons_config block; more here) and the provider argument (missing from the official reference documentation):

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

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

but does not require a google-beta provider definition in providers.tf:

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

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

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

This and the lack of a provider argument from other resource definitions, such as a google_container_node_pool found in container_node_pool.tf, results in the following output from the providers command:

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 状态文件之后.

after an apply command has refreshed the terraform.tfstate state file.

使用 beta 参数对 GCP 资源进行地形改造是更正确且不易出错的方法吗?或者,我应该运行 replace-provider 子命令吗:

Is the more correct and less error-prone method of Terraforming GCP resources with beta arguments? Or, should I run a replace-provider subcommand instead:

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 命令的输出是:

so that the output of the providers command is:

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 中的状态之后?

after an apply command refreshes the state in terraform.state?

推荐答案

应该使用 both googlegoogle-beta 提供者.

You should use both google and google-beta providers.

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

It is safe to use both the google and the google-beta providers within the same providers.tf. Terraform sends requests for any resource requiring the google-beta provider to the Beta endpoint: https://. . .googleapis.com/v1beta1/. . .; I.e., using the google-beta provider is similar to using the beta gcloud group.

应该:

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

  • include both the google and google-beta provider in providers.tf:

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 功能:

  • use the provider argument for every GCP resource: google-beta for any resource that has at least one enabled Beta feature:

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

    google 用于其他所有资源:

    and google for every other resource:

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

  • 在完成上述建议的 both 更改并然后运行 refresh 后,输出providers 命令现在应该如下所示:

    After making both of the suggested changes above and then running a refresh, the output of the providers command should now resemble this:

    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]
    

    应该通读官方文档.对于提供程序版本这里.

    You should read through the official doc. for provider versions here.

    这篇关于具有 Beta 参数的 Google Cloud Platform 资源的正确 Terraform 提供程序配置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆