Resize Google Compute Instance Boot Disk Using Terraform
If you run out of space with your GCP compute instance boot disk and would like to resize it, you can do so by creating a snapshot of the boot disk and then spin up another instance using that snapshot as part of your new boot disk.
I started started out with the default settings for my compute instance
resource "google_compute_instance" "ubuntu-beaver" {
name = "ubuntu-beaver"
machine_type = "n1-standard-1"
zone = "us-east4-b"
allow_stopping_for_update = true
boot_disk {
initialize_params {
image = "ubuntu-1804-lts"
}
}
network_interface {
network = "default"
access_config {}
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
The disk only had 10G of space and I had a bunch of data that I needed to replicate to the new disk. Here’s how I did it:
I created a google_compute_snapshot from the original boot disk:
resource "google_compute_snapshot" "ubuntu-beaver" {
name = "ubuntu-beaver-2018-10-3"
source_disk = "${google_compute_instance.ubuntu-beaver.name}"
zone = "us-east4-b"
}
Then I created a google_compute_disk from the snapshot:
resource "google_compute_disk" "striped-horse" {
name = "striped-horse"
type = "pd-ssd"
zone = "us-east4-b"
size = 500
snapshot = "${google_compute_snapshot.ubuntu-beaver.name}"
}
Then I created a new instance using the google_compute_disk that I just created:
resource "google_compute_instance" "striped-horse" {
name = "striped-horse"
machine_type = "n1-standard-1"
zone = "us-east4-b"
allow_stopping_for_update = true
boot_disk {
source = "${google_compute_disk.striped-horse.name}"
}
network_interface {
network = "default"
access_config {}
}
service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
Now I can tear down the other resources by removing them from my terraform configs.