Konstantin Boyandin
Konstantin Boyandin

Reputation: 113

Terraform with Google Cloud: google_cloud_instance can’t find google_compute_disk

Terraform: 1.0.3

When trying to create google_cloud_disk and google_cloud_instance depending on the disks, snippet from the configuration:

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:

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.

How can I make Terraform detect the google_compute_disk? Using “depends_on” inside instance definition doesn’t help.

Upvotes: 0

Views: 1202

Answers (1)

Thomas Laporte
Thomas Laporte

Reputation: 363

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').

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):

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.

Upvotes: 3

Related Questions