terraform 應用程式錯誤已存在於未觸及的資源上

terraform 應用程式錯誤已存在於未觸及的資源上

我正在按照官方指南開始一個新的 terraform 專案:

https://learn.hashicorp.com/tutorials/terraform/gke?in=terraform/kubernetes&utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS&_ga=2.91746777.2118886489. 63784 9824

我已經設法讓它運行。 (我將其作為提交時觸發的谷歌雲端建置任務的一部分運行)

但是,如果我更改資源中的某些內容(例如,我將“gke_num_nodes”預設值從 2 替換為 1),當我terraform apply再次運行時,這就是我得到的結果:


Plan: 4 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + kubernetes_cluster_host = (known after apply)
  + kubernetes_cluster_name = "workspace-auto-gke"
  + project_id              = "workspace-auto"
  + region                  = "europe-west4"
google_compute_network.vpc: Creating...
│ Error: Error creating Network: googleapi: Error 409: The resource 'projects/workspace-auto/global/networks/workspace-auto-vpc' already exists, alreadyExists
│   with google_compute_network.vpc,
│   on vpc.tf line 15, in resource "google_compute_network" "vpc":
│   15: resource "google_compute_network" "vpc" {

有沒有辦法讓它不嘗試重新創建現有的未受影響的資源?

cloudbuild.json的如下:

{
  "steps": [
    {
      "name": "hashicorp/terraform",
      "entrypoint": "/bin/sh",
      "args": [
        "./cloudbuild/prepare-terraform.sh"
      ]
    }
  ],
  "logsBucket": "gs://my-bucket/logdir",
  "serviceAccount": "projects/my-proj/serviceAccounts/[email protected]"
}

prepare-terraform.sh簡單地

terraform init
terraform plan
terraform apply -auto-approve

答案1

如果您使用 Terraform 建立資源,請執行以下操作不是在 Terraform 之外修改它們。

如果您手動變更某些內容,Terraform 將嘗試將其還原為 HCL 聲明的方式以及 Terraform 儲存的方式。

這就是所謂的陳述性的。您正在嘗試將 Terraform 動態化,這首先導致了使用 Terraform 的失敗。

答案2

terraform 狀態需要儲存在所有建置都可以存取的地方。例如,在Google雲端儲存桶中,如下所示:https://www.terraform.io/docs/language/settings/backends/gcs.html

相關內容