Files
hunfabric/.agents/rules/context-interpolation.md
Ludovico Magnocavallo 21629279a3 Refactor agent documentation and establish core guidelines (#3820)
* update the factories overview

* update agent rules

* update main GEMINI file

* add preferred workflow to GEMINI file
2026-03-28 08:26:00 +01:00

1.4 KiB

trigger
trigger
always_on

Context-Based Interpolation

When designing factory patterns or datasets, you MUST leverage the context-based interpolation system.

A context object variable is used to hold maps of known, existing resource IDs (such as project_ids, folder_ids, networks, iam_principals).

Usage Pattern

  1. Variables: Add a context variable in variables.tf.

    variable "context" {
      description = "Context-specific interpolations."
      type = object({
        project_ids = optional(map(string), {})
        folder_ids  = optional(map(string), {})
      })
      default = {}
    }
    
  2. Locals: Build ctx and ctx_p local variables in main.tf by flattening the var.context object.

    locals {
      ctx = {
        for k, v in var.context : k => {
          for kk, vv in v : "${local.ctx_p}${k}:${kk}" => vv
        }
      }
      ctx_p = "$"
    }
    
  3. Lookups: Apply lookups inside resources.

    project = lookup(local.ctx.project_ids, var.project_id, var.project_id)
    
  4. YAML Interpolation: In factory YAML files, use the $ prefix convention to reference the lookup map keys.

    # Instead of hardcoding the folder ID: parent: folders/1234567890
    parent: $folder_ids:teams/team-a
    

This pattern makes YAML configuration files highly portable across installations and environments by substituting mnemonic keys for hardcoded values.