如何使用 terraform 将 ssh 密钥添加到 GCP 实例?

2024-01-17

因此,我有一个在 Google Cloud Platform 中创建实例的 terraform 脚本,我希望能够让我的 terraform 脚本也将我的 ssh 密钥添加到我创建的实例中,以便我可以通过 ssh 来配置它们。这是我当前的 terraform 脚本。

#PROVIDER INFO
provider "google" {
  credentials = "${file("account.json")}"
  project     = "myProject"
  region      = "us-central1"
}


#MAKING CONSUL SERVERS
resource "google_compute_instance" "default" {
  count    =  3
  name     =  "a-consul${count.index}"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  disk {
    image = "ubuntu-1404-trusty-v20160627"
  }

  # Local SSD disk
  disk {
    type    = "local-ssd"
    scratch = true
  }

  network_interface {
    network = "myNetwork"
    access_config {}
  }
}

我必须添加什么才能让我的 terraform 脚本添加我的 ssh 密钥/Users/myUsername/.ssh/id_rsa.pub?


我认为这样的事情应该有效:

  metadata = {
    ssh-keys = "${var.gce_ssh_user}:${file(var.gce_ssh_pub_key_file)}"
  }

https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys描述了元数据机制,我在以下位置找到了这个示例https://github.com/hashicorp/terraform/issues/6678 https://github.com/hashicorp/terraform/issues/6678

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 terraform 将 ssh 密钥添加到 GCP 实例? 的相关文章

随机推荐