Answers: <p>There was used wrong type for <code>variable</code>, should be:</p>
  
  <pre><code>variable "ssh_keys" {
    type = "map"
    default = {
      "atolkachev" = "ssh-rsa my_key atolkachev"
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-11-16T16:13:42.197Z
Answers: 2019-11-16T16:13:42.197Z
Answers: Alexander Tolkachev
Body: <p>I use Terrafrom and manage access to VMs in GCE by changing VM metadata.</p>
  
  <p>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:</p>
  
  <pre><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}"])
    }
  </code></pre>
  
  <p>When I execute it, I get error:</p>
  
  <pre><code>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}"])
  </code></pre>
  
  <p>I tried to find solution, but failed.</p>
  
Comments: CC BY-SA 4.0
Comments: 2022-08-18T15:55:45.053Z
Comments: Nice hack with join("\n", [for user, key in var.ssh_keys : "${user}:${key}"])
Comments: Baskar Lingam Ramachandran
Tags: <terraform><google-compute-engine>
Title: Use Terraform to manage ssh-keys in GCE
UserDisplayName: Alexander Tolkachev
ThreadText: # Use Terraform to manage ssh-keys in GCE
  
  (Asked by: username_0 on 2019-11-15)
  
  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.
  
  **Tags:** terraform, google-compute-engine
  
  > **Comments:**
  > **username_1**: Nice hack with join("\n", [for user, key in var.ssh_keys : "${user}:${key}"])  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2019-11-16. Score: 1 | Accepted answer
  
  There was used wrong type for `variable`, should be:
  
  ```
  variable "ssh_keys" {
    type = "map"
    default = {
      "atolkachev" = "ssh-rsa my_key atolkachev"
    }
  }
  
  ```
  
  
  ---
  
  