I use Terrafrom and manage access to VMs in GCE by changing VM metadata.

Before I put it to string, but it not good, so I try to put them to variable and use it. But I faced problem. This is my code:

variable "ssh_keys" {
  type = "list"
  default = [
      user = "atolkachev",
      key = "ssh-rsa my_key atolkachev"

resource "google_compute_instance" "instance" {
  zone = "${var.region}-${var.zone}"
  project = "${var.project_name}"
  metadata = {
    ssh-keys = join("\n", [for user, key in var.ssh_keys : "${user}:${key}"])

When I execute it, I get error:

Error: Invalid template interpolation value

  on main.tf line 24, in resource "google_compute_instance" "instance":
  24:     ssh-keys = join("\n", [for user, key in var.ssh_keys : "${user}:${key}"])

I tried to find solution, but failed.

  • Nice hack with join("\n", [for user, key in var.ssh_keys : "${user}:${key}"]) Commented Aug 18, 2022 at 15:55

1 Answer 1


There was used wrong type for variable, should be:

variable "ssh_keys" {
  type = "map"
  default = {
    "atolkachev" = "ssh-rsa my_key atolkachev"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.