3
votes

I tried to stop a GCP VM in terraform using desired_status = "TERMINATED" but I get the following error code:

Error: Unsupported argument

on main.tf line 24, in resource "google_compute_instance" "default": 24: desired_status = "TERMINATED"

An argument named "desired_status" is not expected here.

The full terraform code is:

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
    }
  }
}

provider "google" {
  version = "3.5.0"

  credentials = file("cred.json")

  project = "project-gcp-25625415"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_instance" "default"{
  name         = "terra2"
  machine_type = "f1-micro"
  zone         = "us-central1-a"

  desired_status = "TERMINATED"
  
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral IP
    }
  }

}
2

2 Answers

1
votes

Have a look at the release notes of terraform-provider-google.

To be able to stop and start a GCE VM instances with desired_status field you should use at least version 3.11 or newer of gcp provider:

compute: added the ability to manage the status of google_compute_instance resources with the desired_status field (#4797)

1
votes

Terraform is really designed for declarative operations, stopping an instance is more of a procedural or imperative operation.

Check this out https://docs.infraql.io/language-spec/exec#stopping-a-google-compute-engine-instance, InfraQL enables access to all Google provider methods, can be run non-interactively or interactively,

EXEC compute.instances.stop 
@instance = 'demo-instance-1', 
@project = 'infraql-demo', 
@zone = 'australia-southeast1-a';