【问题标题】:How to I specify companys custom boot image in Terraform script?如何在 Terraform 脚本中指定公司的自定义启动映像?
【发布时间】:2021-05-22 10:40:30
【问题描述】:

我目前正在学习 terraform,但我无法使用我公司的自定义启动映像通过 terraform 设置基本 VM。

如果我要在创建 VM 实例期间在 GCP 页面上手动执行此操作,我会单击“启动磁盘”-> 单击“自定义图像”选项卡 -> 单击“显示图像来源”-> 从中选择特定项目下拉菜单 -> 点击“图片” -> 从下拉菜单中选择正确的图片

但我不知道如何将其转换为 terraform 脚本

provider "google" {
  project = "xxxxxx"
}
resource "google_compute_instance" "xxxxxx" {
  name         = "xxxxxx"
  machine_type = "xxxxxx"
  zone         = "xxxxxx"

  boot_disk {
    initialize_params {
      image = "???"
    }
  }
  metadata_startup_script = ";;"
  network_interface {
    network = "default"
   }
}      

【问题讨论】:

  • 有什么问题?剧本看起来不错。直接换???用你的图片名称。
  • 我尝试用图像名称或项目名称/图像名称替换它,但这些都不起作用。我总是收到错误消息:错误:解析图像名称时出错'project_name/name_of_the_image':找不到图像或家庭 project_name/name_of_the_image 。所需的图像是自定义的,属于不同的项目(不是我试图在其中创建 VM 的同一个项目)

标签: google-cloud-platform terraform virtual-machine terraform-provider-gcp


【解决方案1】:

我不确定这是否有效,但你可以试一试:

由于您需要访问多个项目,我将设置多个 providers 并使用别名:

provider "google" {
  project     = "project-A"
  region      = "us-central1"
  credentials = "path-to-your-service-account"
  alias       = "main_provider"
}

# this is where your image is
provider "google" {
  project     = "project-B"
  region      = "us-central1"
  credentials = "path-to-your-service-account"
  alias       = "image_source_provider"
}

然后使用google_compute_image获取你想要的图片:

data "google_compute_image" "my_image" {
  name    = "name-of-your-image-from-project-B"
  project = "project-B"
}

最后在你的google_compute_instance中使用它:

resource "google_compute_instance" "xxxxxx" {
  name         = "xxxxxx"
  machine_type = "xxxxxx"
  zone         = "xxxxxx"
  provider     = google.main_provider

  boot_disk {
    initialize_params {
      image = data.google_compute_image.my_image.self_link
    }
  }
  metadata_startup_script = ";;"
  network_interface {
    network = "default"
   }
}  

【讨论】:

  • @Pawel - 这解决了您的问题吗?
猜你喜欢
  • 2019-11-13
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多