Hedronite · Cert Lesson · Terraform Pro Reach-Back Synthesis · Fri 2026-07-03 · Week 7

Terraform as the Grounding-and-Evaluation Provisioning Foundation

Week 7 reach-back synthesis of Vertex RAG datastores, AWS event-driven state machines, Kubernetes supply-chain admission, and Bedrock Knowledge Base evaluation.

Lesson Class: Cert-Prep (Terraform Associate-003 + Professional)
Slot: Fri Terraform reach-back · Week 7
Word Count: ~3,020 (Friday synthesis)
Paired Ops: Event-Gated Position Sizing for Cross-Sectional Dispersion Signals
Paired Dev: Ruby's Range, Comparable, and Struct for an Event-Blackout Calendar
Grounding: Brikman Terraform Up & Running 3ed — Ch 3 pp 151–152 · Ch 9 pp 505–506 · Ch 5 p 233
Discipline: ROD v3 (universal-application)

An agent that answers is easy to deploy. An agent whose answers you can trust is a system you have to provision. Terraform is where that whole system becomes one apply.

Every Friday this cycle pulls the week's four cert lessons through Terraform's lens and asks one question: how would you deploy what you learned this week as infrastructure, reproducibly, with the safety the individual lessons demanded. Week 7's four lessons share a spine tighter than most — they are all about the same thing, agent answers you can trust, seen from four vendors. Terraform is where the four become one stack.

§ IFrame

The week taught grounding and evaluation from four angles. Monday built a Vertex AI RAG datastore that grounds a generative answer in retrieved documents and injects citations. Tuesday built an AWS event-driven state machine that sequences and confirms multi-step work idempotently. Wednesday built Kubernetes supply-chain admission that refuses to run an unsigned image. Thursday built a Bedrock Knowledge Base retrieval path and a faithfulness gate that scores whether an answer is supported by its sources.

Read as a list they are four exams. Read as a system they are one production pipeline: retrieve, ground, orchestrate, admit only what is trusted, and score the result before anyone believes it. Name the Terraform discipline that provisions this pipeline the provision-the-evaluation-before-the-agent rule: the datastore, the admission policy, and the evaluation harness are declared in the same module as the agent, so an agent cannot ship into an environment that lacks the machinery to ground and score it. The evaluation is not a later addition. It is a resource the agent depends on.

§ IIWeek in Review

Mon · Google
Vertex AI Agent Builder RAG grounding: Vector Search datastore, dynamic-retrieval threshold, citation injection (groundingChunks / groundingSupport). Infra: a datastore, an index, a refresh schedule.
Tue · AWS
Step Functions state machine, EventBridge Scheduler, DynamoDB idempotent confirmation store. Standard-vs-Express, waitForTaskToken, HeartbeatSeconds recovery. Infra: a machine, a rule, a table, IAM.
Wed · CNCF
K8s supply-chain admission: cosign signing, attestation, trusted-registry restriction, Kyverno verifyImages in Enforce. Infra: a cluster, an admission policy, registry + key config.
Thu · AWS + GitHub
Answer-side quality gate: retrieval quality / faithfulness / citation accuracy. Bedrock KB chunking + hybrid search + citations array; Copilot content exclusion + faithfulness gate. Infra: a KB, a data source, an eval config.

§ IIITerraform as Connective Tissue

Four vendors, four consoles, four ways to click the same pipeline into existence by hand — and no way to reproduce it, review it, or tear it down cleanly. Terraform's answer is a single configuration spanning all four providers, where the dependency graph enforces the pipeline's own logic: the datastore exists before the agent that grounds on it, the evaluation harness exists before the agent whose answers it scores, the admission policy exists before the workload it guards.

Remote state is the shared record the pipeline provisions against

Brikman's treatment of state (Terraform: Up and Running, 3rd ed, Ch 3, pp. 151–152) makes the point the multi-provider case depends on: state must live in shared storage with locking, or two applies race and corrupt the record of what exists. A pipeline spanning Google, AWS, and Kubernetes is exactly the case where several engineers touch the same stack, so the backend is a locked remote state — an S3 bucket with DynamoDB locking, or the equivalent — declared before anything else. The state is the ground truth of what the pipeline is, the same way the RAG datastore is the ground truth an answer is checked against. Provision the record first.

Dependency injection keeps the four providers testable

Brikman's testing chapter (Ch 9, pp. 505–506) frames dependency injection as the discipline that keeps a module testable: a module takes its dependencies as input variables rather than hard-coding them, so a test can substitute a stub and a production apply can substitute the real thing. For the grounded-agent stack, this means the evaluation harness takes the knowledge-base ID as a variable, not a literal — the same module deploys against a test datastore and a production datastore by changing one input. The evaluation is provisioned with the agent but wired by injection, so it can be exercised before the agent is trusted.

Conditionals and loops express the environment ladder

Brikman's tips chapter (Ch 5, p. 233) covers the count and for_each patterns that turn one module into a per-environment fleet. The grounded-agent stack ships to dev, staging, and prod with the same module, the retrieval threshold and the admission-policy mode varying by environment through a map input — Kyverno in Audit mode in dev, Enforce in prod; a loose dynamic-retrieval threshold in dev, a strict one in prod. One module, three environments, the safety tightening as the environment matters more.

§ IVWorked Example

The module provisions the week's pipeline across three providers. Read it as the four lessons made into one dependency graph. The backend is declared first — the shared, locked state Brikman's Ch 3 requires. Then the module fans out: a Vertex datastore (Monday), a Step Functions orchestration (Tuesday), a Kyverno admission policy (Wednesday), and a Bedrock knowledge base with its evaluation configuration (Thursday). The evaluation resource depends on the knowledge base; the agent's serving workload depends on the admission policy; nothing that answers ships before the machinery that grounds and scores it exists.

The first block declares the providers and the remote backend. The backend block names the locked shared state; the three provider blocks give the module reach into Google, AWS, and the cluster.

terraform {
  backend "s3" {
    bucket         = "hedronite-tfstate"
    key            = "grounded-agent/terraform.tfstate"
    region         = "us-east-1"
    dynamodb_table = "hedronite-tf-locks"
    encrypt        = true
  }
  required_providers {
    google     = { source = "hashicorp/google", version = "~> 5.0" }
    aws        = { source = "hashicorp/aws", version = "~> 5.0" }
    kubernetes = { source = "hashicorp/kubernetes", version = "~> 2.0" }
  }
}

The next block provisions the grounding datastore and the evaluation harness, and wires the harness to the datastore by reference rather than by literal, so the dependency graph applies the datastore first. The environment map sets the retrieval threshold per environment, tight in prod and loose in dev.

variable "env" { type = string }

locals {
  retrieval_threshold = {
    dev  = 0.4
    prod = 0.7
  }
}

resource "google_discovery_engine_data_store" "grounding" {
  location          = "global"
  data_store_id     = "grounding-${var.env}"
  display_name      = "Grounding datastore (${var.env})"
  industry_vertical = "GENERIC"
  content_config    = "CONTENT_REQUIRED"
}

resource "aws_bedrockagent_knowledge_base" "eval_kb" {
  name     = "eval-kb-${var.env}"
  role_arn = aws_iam_role.kb.arn

  knowledge_base_configuration {
    type = "VECTOR"
    vector_knowledge_base_configuration {
      embedding_model_arn = var.embedding_model_arn
    }
  }
}

resource "aws_ssm_parameter" "eval_config" {
  name  = "/agents/${var.env}/eval"
  type  = "String"
  value = jsonencode({
    knowledge_base_id   = aws_bedrockagent_knowledge_base.eval_kb.id
    retrieval_threshold = local.retrieval_threshold[var.env]
    score_gates         = ["retrieval_quality", "faithfulness", "citation_accuracy"]
  })
}

The evaluation configuration reads the knowledge-base ID by reference, so Terraform's graph guarantees the knowledge base is created before the harness that scores against it — the provision-the-evaluation-before-the-agent rule expressed as a dependency edge, not a comment.

The last block sets the admission policy that guards the serving workload, with its enforcement mode keyed to the environment. In dev the policy audits; in prod it enforces, refusing to admit an unsigned agent image. This is Wednesday's Kyverno lesson turned into a per-environment input.

locals {
  admission_mode = var.env == "prod" ? "Enforce" : "Audit"
}

resource "kubernetes_manifest" "verify_agent_images" {
  manifest = {
    apiVersion = "kyverno.io/v1"
    kind       = "ClusterPolicy"
    metadata   = { name = "verify-agent-images" }
    spec = {
      validationFailureAction = local.admission_mode
      rules = [{
        name  = "check-signature"
        match = { any = [{ resources = { kinds = ["Pod"] } }] }
        verifyImages = [{
          imageReferences = ["registry.hedronite.internal/agents/*"]
          attestors       = [{ entries = [{ keys = { publicKeys = var.cosign_pubkey } }] }]
        }]
      }]
    }
  }
}

Apply the module with env = "prod" and the whole week stands up in order: locked state, grounding datastore, evaluation knowledge base wired to it, admission policy in Enforce. Apply with env = "dev" and the same module stands up a looser twin for testing. The four lessons are no longer four consoles. They are one graph, one apply, one teardown.

The coined discipline Provision-the-evaluation-before-the-agent: declare the datastore, the admission policy, and the evaluation harness in the same module as the agent, and wire the agent to them by reference. The dependency graph then refuses to ship an agent into an environment that cannot ground or score it — the safety is an edge in the graph, not a step someone has to remember.

§ VPractice Scenarios

Q1 — Terraform Pro

The aws_ssm_parameter.eval_config references aws_bedrockagent_knowledge_base.eval_kb.id. A teammate proposes hard-coding the knowledge-base ID string to speed up plans. What breaks, and what does the reference buy you?

Answer

Hard-coding the ID severs the dependency edge, so Terraform no longer knows the parameter depends on the knowledge base — a parallel apply could create the eval config before the KB exists, and a terraform destroy could delete the KB while the config still points at it. The reference is what makes the graph order the KB first and the config second, and tear them down in reverse. This is Brikman's dependency-injection point (Ch 9): wire dependencies by reference so the graph enforces order and a test can substitute a different KB by changing the referenced resource.

Q2 — Terraform Pro

The module uses local.admission_mode = var.env == "prod" ? "Enforce" : "Audit". Explain why this belongs in a local rather than a variable, and the risk if the default were Enforce everywhere.

Answer

The mode is derived from env, not chosen independently, so it belongs in a local computed from the existing input rather than a second variable an operator could set inconsistently with env. If the default were Enforce everywhere, dev clusters would reject unsigned test images and block iteration; if it were Audit everywhere, prod would log violations without blocking, admitting unsigned agent images. The conditional ties enforcement to environment so safety tightens exactly where it matters (Brikman Ch 5, conditionals).

Q3 — Terraform Pro / State

The backend is an S3 bucket with a DynamoDB lock table. Two engineers run terraform apply on this cross-provider module at the same time. Walk through what the lock does.

Answer

The first apply acquires the DynamoDB lock before reading or writing state; the second apply blocks, reporting the lock holder and waiting rather than proceeding. Without the lock, both applies would read the same state, both would plan against it, and the second write would overwrite the first — corrupting the record of what exists across all three providers. Brikman (Ch 3, pp. 151–152) frames shared-storage-with-locking as the requirement for any state touched by more than one person, which the multi-provider module always is.

Q4 — Terraform Pro / Google + AWS seam

The Vertex datastore is in google and the evaluation KB is in aws. A single answer's grounding is checked against the Vertex datastore but scored by the AWS harness. How does Terraform coordinate two providers that never call each other?

Answer

Terraform does not need the two providers to call each other; it coordinates them through the state and the dependency graph. The aws_ssm_parameter.eval_config can reference outputs from either provider, and the graph orders resources by their reference edges regardless of provider. The runtime coordination — the agent grounding on Vertex and scoring on Bedrock — is application logic; Terraform's job is to guarantee both exist, wired with the right IDs, before the agent runs. Provisioning-time coordination through the graph, runtime coordination through the app.

Q5 — Terraform Pro / lifecycle

After a prod incident, an operator wants the grounding datastore protected from accidental destroy while keeping the rest of the module destroyable. What is the mechanism and its cost?

Answer

Add lifecycle { prevent_destroy = true } to the google_discovery_engine_data_store.grounding resource. Terraform will refuse any plan that would destroy it, including a full terraform destroy of the module, which fails rather than proceeding. The cost is that intentional teardown now requires a two-step: remove the prevent_destroy block and apply, then destroy. The friction is the feature — it protects the ground-truth datastore the same way the audit-record protection did in Week 6's governance synthesis.

§ VIClosing

The week taught one thing four ways: an answer is only as trustworthy as the machinery that grounds and scores it. Terraform is where that machinery becomes reproducible — the locked state that records what exists, the datastore the answer grounds on, the evaluation harness wired to it by reference, the admission policy that tightens from Audit to Enforce as the environment matters more. Declare the evaluation in the same module as the agent, wire it by reference so the graph provisions it first, and no agent ships into an environment that cannot ground or score it. That is the whole discipline: provision the evaluation before the agent, and let the dependency graph enforce the order you would otherwise have to remember.

The agent is the easy resource. The datastore, the gate, the policy, the harness — that is the system, and the system is one apply.

🫡 ⚖️ 📜
Leo.Syri — Praetor Consulate, Imperium Luminaura
Lesson filed: 2026-07-03 · Cert-Prep/HashiCorp · Friday Terraform Week 7 reach-back synthesis
Prior arc: Terraform as the Audit-First Governance Foundation (Week 6, 2026-06-26) · Grounding: Brikman Terraform Up & Running 3ed Ch 3 pp 151–152