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

Viewed 1411

On GCP, I'm trying to run the Cloud Run service "renderer" with the Terraform code below and in this code, I concatenate "gcr.io/", the variable var.project_id and "/renderer:latest" using 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
  }
}

Then, when I applied this code above, I got this error below:

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.

Is it impossible to use plus(+) for concatenation? and if it's impossible, how do I do concatenation in Terraform?

2 Answers

Yes, it's impossible to use plus(+) for concatenation in Terraform. So, you need to use "Interpolation Syntax" ${var.project_id} for concatenation as shown below instead of 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
  }
}

In addition, you cannot use plus(+) for concatenation but you can use plus(+) for calculation. For example, your Terraform code has "percent" in "traffic" block:

traffic {
  percent         = 100
  latest_revision = true
}

Then, you can add 30 to 100 with plus(+):

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

And, even the numeric strings "100" and "30" can be calculated as Number types:

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

Had a similar issue when trying to create an iam group using Terraform.

The issue was caused by an indentation:

variable "iam_path" {
  type        = string
  description = "(Optional, default " / ") Path in which to create the group."
}

I was using double quotes " / " within the description double quotes.

All I had to do was remove the double quotes for " / " to be single quotes ' / ' or remove the quotes altogether. So we will have:

variable "iam_path" {
  type        = string
  description = "(Optional, default ' / ') Path in which to create the group."
}

OR

variable "iam_path" {
  type        = string
  description = "(Optional, default  / ) Path in which to create the group."
}
Related