Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop GCP vm instances using terraform

I am new to Terraform. How to stop GCP vm instances using terraform? I have tried changing status of the vm instance, it's available for AWS but couldn't find the way to do it for GCP.

like image 738
aritra Avatar asked Oct 29 '25 15:10

aritra


1 Answers

Edit

Since version v3.11.0 of Google provider (released 2020/03/02), it is possible to shutdown and start a Compute instance with desired_status field :

  • compute: added the ability to manage the status of google_compute_instance resources with the desired_status field

Just declare in your Terraform resource :

resource "google_compute_instance" "default" {
  name         = "test"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  [...]

  desired_status = "TERMINATED"
}

And apply your changes. If your instance was running before, it should be shut down. This PR shows the modifications that have been added, if you are interested to take a look. The desired_status can either take RUNNING or TERMINATED values.

Previous answer (as of 2019/10/26)

As of the time of the question (2019/09/18), with the latest Google provider available then (version v2.15.0), this is not possible to update the status of a Google Compute instance.

The following issue is opened on the Google Terraform provider on Github :

  • google_compute_instance should allow to specify instance state #1719

There is also a Pull Request to add this feature :

  • ability to change instance_state #2956

But unfortunately, this PR seems to be stale (not updated since 2019/03/13).

like image 123
norbjd Avatar answered Oct 31 '25 10:10

norbjd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!