terraform google 来自另一个项目的自定义图像

terraform google custom image from another project

我知道这可以通过控制台实现,但是如何从 terraform 中的另一个项目初始化引导磁盘映像?

这是我目前拥有的,但它说找不到图像:

  boot_disk {
    initialize_params {
    image = "cof-ubuntu1604-180124"
    }
  }

图片不是图片名称,是图片的URI

data "google_compute_image" "my_image" {
  name = "cof-ubuntu1604-180124"
  # could also use family = "family-name"
}

resource "google_compute_instance" "default" {
  name         = "test"
  ...

  boot_disk {
    initialize_params {
      image = "${data.google_compute_image.my_image.self_link}"
    }
  }

  ...

}

参考:

google_compute_image

您在字段 "image" 上使用的磁盘地址不正确。

首先您需要确保可以从您的项目中访问图像,请参阅 here for info about sharing images

然后,您使用的 "image" 变量必须指向正确的 URI。它看起来像这样:

"selfLink": "projects/ndjanuary-190908/global/images/ffs12354",

您可以使用方法 "compute.images.get" 从计算引擎 API 获取该信息,我的请求如下所示:

GET https://www.googleapis.com/compute/v1/projects/ndjanuary-190908/global/images/ffs12354?key={YOUR_API_KEY}

这里是 link 相关的 API 浏览器:

https://developers.google.com/apis-explorer/#search/image/m/compute/v1/compute.images.get

提供的答案都非常有帮助,解决了我的问题。 API 资源管理器的使用对于确定正确的 URI 非常重要。