Skip to content

refactor(compute-engine): support multiple VMs via instances map#4

Merged
Lexxick merged 6 commits into
mainfrom
fix/compute-engine-multi-vm
Jun 16, 2026
Merged

refactor(compute-engine): support multiple VMs via instances map#4
Lexxick merged 6 commits into
mainfrom
fix/compute-engine-multi-vm

Conversation

@Lexxick

@Lexxick Lexxick commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Summary

Reworks compute-engine to manage many VMs via an instances map (for_each), and stops the module from hard-coding environment identity. Mirrors the github component's repositories pattern.

Changes

  • Multi-VM: single hard-coded instance → instances map fanned out with for_each. VM name = <env>-<key> (e.g. postizdev-postiz). Per-VM spec (machine_type, boot_image, boot_disk_size_gb, zone, assign_public_ip, startup_script, network_tags) moved into each map entry, all optional() with cost-safe defaults.
  • IAM: OS Login + IAP grants fan out member × VM via setproduct on stable keys — adding a VM or member never reindexes existing grants.
  • Env identity out of the module: project_id is now required (no dev-project default → forgotten value fails loudly); access_members defaults to [] (no SSH) instead of named engineers.
  • Outputs: collapse to a single instances map keyed by VM key (name, instance_id, internal_ip, zone, ssh_command).
  • Docs: compute-engine/README.md + CHANGELOG [Unreleased] updated; fixed a stale VM-name string in the instances var description.

Type

  • feat - [ ] fix - [x] refactor - [ ] docs - [ ] ci - [ ] chore

⚠️ Breaking: state re-keys google_compute_instance.thisthis["<key>"] (IAM members too) — consumers must terragrunt state mv or recreate, and must now set project_id + access_members explicitly.

How to Verify

  1. cd compute-engine/terraform && terraform fmt -check && terraform validate → clean.
  2. In infra-environments-dev, set instances = { ... } + project_id, run terragrunt plan — one VM per map key, no external IP, OS Login + IAP grants per member × VM.
  3. gcloud compute ssh <env>-<key> --tunnel-through-iap (from instances[<key>].ssh_command).

Lexxick and others added 4 commits June 15, 2026 19:14
The original component hardcoded a single google_compute_instance, so
the
module could only ever manage one VM. Rework it to fan out over an
instances map (for_each), matching the github component's pattern:
per-VM
spec fields carry module defaults and are overridden per entry, IAM
grants
fan out over member x instance, and outputs become a per-instance map.
project_id and access_members gain defaults (dev project + the two
engineers) since they are stable across VMs
…empty

A reusable module should know how to build a VM, not where or who. Drop the
dev-project default on project_id (now required — a forgotten value fails loudly
instead of silently provisioning into the wrong project) and default
access_members to [] (no SSH) instead of two named engineers. Cost-safe "how"
defaults (e2-micro, debian-12, 20 GB) unchanged. Refresh README + CHANGELOG for
this and the prior multi-VM instances-map rework.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Drop the dropped `compute-engine` infix; name is `<env>-<key>` (matches main.tf and README).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Lexxick Lexxick requested a review from opariffazman as a code owner June 16, 2026 03:00
@opariffazman

Copy link
Copy Markdown
Contributor

@Lexxick — review follow-ups to fix (rest LGTM, fmt/validate pass):

Docs (2 nits, output keying):

  • CHANGELOG.md [Unreleased] says outputs are "keyed by VM name" — actually keyed by the map key (short name), not the full <env>-<key> VM name. Change to "keyed by VM key" to match outputs.tf (for k, vm ... : k =>).
  • compute-engine/README.md "What it creates" header reads as if the output key is the VM name. Same fix — it's the short key, not <env>-<key>.

Optional — input validation (parity with github.repositories, which has a validation block):
VM name <env>-<key> must be RFC1035 (lowercase, digits, hyphens, ≤63, start w/ letter). A bad key (uppercase, _, too long) fails at apply, not plan. Cheap guard on the instances var:

validation {
  condition     = alltrue([for k in keys(var.instances) : can(regex("^[a-z][a-z0-9-]*$", k))])
  error_message = "Each instances key must be lowercase alphanumeric/hyphen and start with a letter (RFC1035)."
}

Non-blocking. Docs worth fixing before merge; validation is your call.

Lexxick added 2 commits June 16, 2026 12:10
…s keys

Review follow-ups from #4:
- CHANGELOG/README: outputs are keyed by the map key (short name), not
the
  full <env>-<key> VM name.
- Add RFC1035 validation on instances keys so a bad key fails at plan,
not
  apply (parity with github.repositories).
Outputs are keyed by the map key (short name), not the <env>-<key> VM
name —
consistency with compute-engine README + CHANGELOG.
@Lexxick Lexxick merged commit 31fb330 into main Jun 16, 2026
3 checks passed
@Lexxick Lexxick deleted the fix/compute-engine-multi-vm branch June 16, 2026 04:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants