Terraform:将文件复制到 GCP 计算实例

jon*_*mix 6 google-cloud-platform terraform devops

这肯定不会那么复杂吧?我尝试过很多方法,但都失败了。

简单地使用本来file provisioner是最简单的选择,但没有效果。问题是,计算实例没有配置为标准的密码,并且没有密钥。

因此,这导致我尝试这样的事情:

resource "google_compute_instance" "test-build" {
...
  metadata {
    ssh-keys = "jon:${file("./gcloud_instance.pub")}"
  }
...

provisioner "file" {
  source      = "test/test_file"
  destination = "/tmp/test_file.txt"

  connection {
    type     = "ssh"
    private_key = "${file("./gcloud_instance")}"
    agent = "false"
  }
}
Run Code Online (Sandbox Code Playgroud)

再次,无济于事。(仅供参考,密钥对是我创建的,并且我确认公钥已推送到计算实例)

我什至尝试将其拆分为模块,然后一个接一个地运行..但似乎我们无法在 null_resource 中使用文件提供程序。所以这也行不通。

有没有人找到一种方法可以有效地做到这一点?

jon*_*mix 7

取消那个,我解决了..如果我添加用户,它会有所帮助!回答这个问题,而不是删除它,因为我在网上找不到太多其他类似的例子,所以它可能对其他人有用。

resource "google_compute_instance" "test-build" {
  project                   = "artifactory-staging"
  name                      = "file-transfer-test"
  machine_type              = "n1-standard-2"
  zone = "europe-west3-b"
  allow_stopping_for_update = "true"

  boot_disk {
    initialize_params {
      image = "centos-7-v20181210"
    }
  }
  network_interface {
    subnetwork         = "default"
    subnetwork_project = "artifactory-staging"
    access_config      = {}
  }
  metadata {
    ssh-keys = "jon:${file("./creds/gcloud_instance.pub")}"
  }

provisioner "file" {
  source = "creds/test_file"
  destination = "/tmp/test_file"

  connection {
    type = "ssh"
    user = "jon"
    private_key = "${file("./creds/gcloud_instance")}"
    agent = "false"
  }
}
}
Run Code Online (Sandbox Code Playgroud)