首页 > 解决方案 > Terraform:将文件复制到 GCP 计算实例

问题描述

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

简单地使用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"
  }
}

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

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

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

标签: google-cloud-platformterraformdevops

解决方案


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

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"
  }
}
}

推荐阅读