无法使用 terraform 停止 GCP VM

Cannot stop a GCP VM using terraform

我尝试使用 desired_status = "TERMINATED" 在 terraform 中停止 GCP 虚拟机,但出现以下错误代码:

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.

完整的地形代码是:

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
    }
  }

}

查看 terraform-provider-google 的发行说明。

为了能够停止和启动带有 desired_status 字段的 GCE VM 实例,您应该至少使用 version 3.11 或更新的 gcp 提供程序:

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

Terraform 实际上是为声明式操作而设计的,停止实例更像是一种过程性或命令式操作。

检查一下 https://docs.infraql.io/language-spec/exec#stopping-a-google-compute-engine-instance,InfraQL 允许访问所有 Google 提供程序方法,可以是 运行 非交互或交互,

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