Review GCP VM API scopes and IAM permissions

Distinguish cloud-platform scope from actual IAM permissions and apply least privilege.

Description

Using cloud-platform on a Compute Engine VM still limits its service account to the permissions granted through IAM.

Potential impact

Broad IAM roles can expose resources the workload does not need. Narrowing OAuth scopes can also block required API writes.

Remediation

Grant only the IAM roles the workload needs. Google recommends least-privilege IAM with cloud-platform. If policy requires narrower scopes, schedule downtime. Set allow_stopping_for_update = true so Terraform can stop the VM, or set desired_status = "TERMINATED". Test the required API calls once the VM is running again.

Examples

The example removes cloud-platform and keeps userinfo-email, compute-ro and storage-ro. Configure IAM separately and replace the sample image and network with your deployment values.

Before

hcl
resource "google_compute_instance" "vm" {
  name         = "test"
  machine_type = "e2-medium"
  zone         = "us-central1-a"

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

  network_interface {
    network = "default"

    access_config {}
  }

  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro", "cloud-platform"]
  }
}

After

hcl
resource "google_compute_instance" "vm" {
  name         = "test"
  machine_type = "e2-medium"
  zone         = "us-central1-a"

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

  network_interface {
    network = "default"

    access_config {}
  }

  service_account {
    scopes = ["userinfo-email", "compute-ro", "storage-ro"]
  }
}

References