简体   繁体   English

带有 Google Cloud 的 Terraform:google_cloud_instance 找不到 google_compute_disk

[英]Terraform with Google Cloud: google_cloud_instance can’t find google_compute_disk

Terraform: 1.0.3地形:1.0.3

When trying to create google_cloud_disk and google_cloud_instance depending on the disks, snippet from the configuration:当尝试根据磁盘创建 google_cloud_disk 和 google_cloud_instance 时,请从配置中提取片段:

resource "google_compute_disk" "tst-disk1" {
    name         = "tst-disk1"
    image        = "debian-cloud/debian-10-buster-v20210217"
    zone         = "us-central1-a"
    type         = "pd-balanced"
    physical_block_size_bytes = 4096
}

resource "google_compute_instance" "tst-instance1" {
    name         = "tst-instance1"
    machine_type = "e2-micro"
    zone         = "us-central1-a"
    
    boot_disk {
        device_name = "tst-disk1"
        initialize_params {
            size = "10"
            image = "https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-a/disks/tst-disk1"
        }
    }
}

However, when I do 'terraform apply', the instance isn't created:但是,当我执行“terraform apply”时,不会创建实例:

Error: Error resolving image name 'https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-a/disks/tst-disk1': Could not find image or family https://www.googleapis.com/compute/v1/projects/projectname/zones/us-central1-a/disks/tst-disk1
│ 
│   with google_compute_instance.tst-instance1,
│   on instances.tf line 91, in resource "google_compute_instance" "tst-instance1":
│   91: resource "google_compute_instance" "tst-instance1"

But when I do 'gcloud compute disks list --format=yaml', the “absent” disk is listed, its selfLink matches the problem one exactly.但是当我做'gcloud compute disks list --format=yaml'时,列出了“不存在”的磁盘,它的selfLink与问题一完全匹配。

How can I make Terraform detect the google_compute_disk?如何让 Terraform 检测 google_compute_disk? Using “depends_on” inside instance definition doesn't help.在实例定义中使用“depends_on”没有帮助。

The error message states that it cannot find an image named "[...]/zones/us-central1-a/disks/tst-disk1", which is expected because this is the URI of a disk, not an actual image (such as 'debian-cloud/debian-10-buster-v20210217').错误消息指出它找不到名为“[...]/zones/us-central1-a/disks/tst-disk1”的图像,这是预期的,因为这是磁盘的 URI,而不是实际图像(例如“debian-cloud/debian-10-buster-v20210217”)。

However providing a valid image here would result in creating a new boot disk for that instance.但是,在此处提供有效映像将导致为该实例创建新的启动磁盘。 In your case you probably don't want to create a new disk but instead use the one you created just before.在您的情况下,您可能不想创建新磁盘,而是使用您之前创建的磁盘。

In order to use google_compute_disk.tst-disk1 as a boot disk for google_compute_instance.tst-instance1 you can declare it that way (using the source argument in the boot_disk block):为了使用 google_compute_disk.tst-disk1 作为 google_compute_instance.tst-instance1 的启动盘,您可以这样声明(使用 boot_disk 块中的source参数):

resource "google_compute_disk" "tst-disk1" {
    name         = "tst-disk1"
    image        = "debian-cloud/debian-10-buster-v20210217"
    zone         = "us-central1-a"
    type         = "pd-balanced"
}

resource "google_compute_instance" "tst-instance1" {
    name         = "tst-instance1"
    machine_type = "e2-micro"
    zone         = "us-central1-a"
    
    boot_disk {
        source = google_compute_disk.tst-disk1.self_link
    }
}

This creates an explicit dependency between the two resources, as Terraform will first need to create tst-disk1, to then evaluate the self_link used in tst-instance1.这会在两个资源之间创建显式依赖关系,因为 Terraform 首先需要创建 tst-disk1,然后评估 tst-instance1 中使用的 self_link。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM