Chapter Five

The Language — Variables, Outputs, and Expressions

The point where the Hatch pipeline stops being hardcoded. Inputs become variables, derived values become locals, the module interface becomes outputs — and expressions, functions, and dynamic blocks are the grammar that ties them together.

7 topics

Up to here the Hatch ingest pipeline has been a single configuration with its project, region, bucket names, and topic baked in as string literals. That works for exactly one environment. The moment you want the same pipeline in hatch-pipeline-dev, staging, and prod, those literals have to come out of the body and become inputs — and the values the config derives from them have to be named and reused instead of copy-pasted.

This chapter is the Terraform language itself: how you parameterize a configuration with input variables, constrain them with types and validation, expose results with outputs, factor derived values into locals, and write the expressions, functions, and dynamic blocks that compute everything in between. None of it is GCP-specific — it is the part of the skill that carries to every provider — but every example is the Hatch pipeline on Google Cloud.

Topics in This Chapter

Topic 28
Input Variables
The parameters of a configuration. Declaring with var blocks, defaults versus required, the .tfvars file per environment, and the strict precedence order from -var down to default.
LanguageVariables
Topic 29
Variable Types and Validation
A type is a contract checked at plan time. Primitives, list versus set versus map, object types with optional fields, and validation blocks that reject bad input before the API does.
LanguageTypes
Topic 30
Outputs
A configuration's return values, and the only way data escapes a module. Declaring outputs, the sensitive flag, depends_on for side-effect completion, and computed output shapes.
LanguageOutputs
Topic 31
Local Values
A named expression computed once and reused. Local versus variable, the common-labels pattern merged into every resource, and when a local adds indirection instead of clarity.
LanguageLocals
Topic 32
Expressions and Operators
Everything to the right of an equals sign. Arithmetic and logical operators, references that imply dependency order, string templates and heredocs, and the conditional that feeds count and for_each.
Language
Topic 33
Built-in Functions
The built-in library and how to compose it. jsonencode for GCP IAM and policy documents, templatefile for external files, try/can/coalesce for fallbacks, and the no-user-functions limit.
LanguageFunctions
Topic 34
Dynamic Blocks
Generating repeated nested blocks from a collection — firewall allow rules, Cloud Run env blocks. The mechanism, the iterator, the empty-collection case, and why a for_each resource is often the better tool.
Language