Charlie Roffe

Principal Architect & Engineering Director · Cloud & AI

Madrid, Spain · LinkedIn · GitHub

ADR-032: Real Terraform in the Request Path

Context

The Terraform demo used to play back hardcoded output, so changing an input didn't change the plan. Running real Terraform for anonymous visitors raises obvious risks: it needs a provider, credentials and a network, visitors could submit hostile HCL, and it runs as a child process in a shell-less distroless container on in-memory /tmp.

Decision

Run the genuine terraform binary per request, fully offline, against HCL the server generates from validated inputs.

1. Generated HCL only: a strict schema (one of four modules, project-id and region patterns, two booleans) feeds the same main.tf the page shows. Visitor-written HCL is never run.
2. Offline: plan is init -backend=false, validate and plan -refresh=false on create-only config; apply is terraform test with a mock google provider. The image carries a pinned Terraform release and a local mirror of a pinned google provider, so not even init touches the network.
3. A contained child process: no shell, an allowlisted environment with no server secrets, a 20-second kill, 600 output lines, a fresh temp directory per run, and abort on disconnect.
4. Bounded load: two runs per instance (others get 503 with Retry-After), the shared per-IP limit and App Check, and the FEATURE_DEMO_TERRAFORM flag.
5. Output streams to the page over Server-Sent Events.

Amended 2026-10-30, start from a diagram: an uploaded photo or sketch of an architecture is mapped by Gemini onto the demo's own inputs (one of the four modules, a region and two switches). The model never writes HCL: the server's generator does, from those validated inputs, and the visitor then runs the real plan. The image is downsized in the browser, sent to Gemini only, and not stored or logged.

Consequences

Benefits:
* The demo is honest: every input change produces a real plan.
* No credentials, cloud calls or state; a run cannot touch real infrastructure.
* The same pinned Terraform and provider versions run in CI, the demo and the site's own infrastructure.

Trade-offs:
* A larger image and a wider runtime surface than Node alone (see ADR-022).
* Each run costs CPU, memory and a few seconds.
* The client and server copies of the HCL template must stay identical, and upgrades must bump the Dockerfile and server versions together.

Alternatives considered

1. Keep the scripted playback: Rejected — it demonstrates nothing and drifts from real output.
2. Run against a real sandbox GCP project: Rejected — credentials in the request path, real API calls and spend, and cleanup of abandoned resources.
3. Accept HCL typed by the visitor: Rejected — arbitrary HCL can read files, run programs through providers and exhaust resources.
4. A separate sandbox service for Terraform: Rejected for now — more infrastructure and latency than the traffic justifies.

Architecture Decision Records