(Terraform) 左操作数的值不合适:需要一个数字

(Terraform) Unsuitable value for left operand: a number is required

GCP 上,我正在尝试 运行 云 运行 服务“renderer”下面的 Terraform 代码和这段代码中,我连接了 "gcr.io/",变量 var.project_id"/renderer:latest" 使用 plus(+):

resource "google_cloud_run_service" "renderer" {
  name     = "renderer"
  location = "asia-northeast1"

  template {
    spec {
      containers {
        image = "gcr.io/" + var.project_id + "/renderer:latest" // Here
      }
      service_account_name = google_service_account.renderer_identity.email
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

然后,当我应用上面的代码时,出现以下错误:

Error: Invalid operand
 
  on 2nd.tf line 38, in resource "google_cloud_run_service" "renderer":
  38:         image = "gcr.io/" + var.project_id + "/renderer:latest"
 
Unsuitable value for left operand: a number is required.


Error: Invalid operand
 
  on 2nd.tf line 38, in resource "google_cloud_run_service" "renderer":
  38:         image = "gcr.io/" + var.project_id + "/renderer:latest"
 
Unsuitable value for right operand: a number is required.

难道不能用plus(+)进行连接吗?如果不可能,我该如何在 Terraform 中进行 concatenation?

是的,不可能在 Terraform 中使用 plus(+) 进行 concatenation。因此,您需要使用 "Interpolation Syntax" ${var.project_id} 来进行 concatenation,如下所示,而不是 加号(+):

resource "google_cloud_run_service" "renderer" {
  name     = "renderer"
  location = "asia-northeast1"

  template {
    spec {
      containers {
        image = "gcr.io/${var.project_id}/renderer:latest" // Here
      }
      service_account_name = google_service_account.renderer_identity.email
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
}

此外,您不能使用 plus(+) 进行 concatenation,但可以使用 plus(+) 用于 计算 。例如,您的 Terraform 代码在 "traffic" 块中有 "percent"

traffic {
  percent         = 100
  latest_revision = true
}

然后,您可以使用 plus(+)30 添加到 100:

traffic {
  percent         = 100 + 30 // 130
  latest_revision = true
}

而且,即使数字字符串“100”“30”也可以计算为数字类型:

traffic {
  percent         = "100" + "30" // 130 (Number type)
  latest_revision = true
}