teluzia.Cloud Engineer · Day 87
CHAPTER 13 · TERRAFORM FUNDAMENTALS

Data sources, variables and locals

4–5 hours including independent practice3 topics
LET’S MAKE THIS SIMPLE

What you’re learning today

Variables are inputs from the customer, locals are calculations in the blueprint, and data sources inspect existing surroundings. Variables make configuration adjustable with explicit types, defaults, descriptions, and optional validation. Sensitive marking affects display but is not encryption. Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment. Locals name derived expressions within a module. They are not externally settable inputs and do not persist independently as remote resources. Use them for repeated naming, tags, and computations where a descriptive name improves readability without hiding meaningful resource relationships.

Think of it this way

Variables are inputs from the customer, locals are calculations in the blueprint, and data sources inspect existing surroundings.

TOPIC 01 / 03Step by step

Input variables and validation

Variables make configuration adjustable with explicit types, defaults, descriptions, and optional validation. Sensitive marking affects display but is not encryption. Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment. For example, a region variable has type string; a replicas variable has a validation requiring a positive integer.

WORKED EXAMPLE
A region variable has type string; a replicas variable has a validation requiring a positive integer.

A module accepts replicas as an integer and environment as a string. At plan time, callers supply values through reviewed files or flags. Type constraints reject incompatible shapes; validation can reject a numeric value outside the module's intended range. The resulting plan then shows how those accepted choices affect resources.

An input variable is a caller-supplied parameter. A default supplies a fallback when permitted. Validation expresses additional rules, such as requiring replicas greater than zero. Document units and meaning: a number called timeout is ambiguous without seconds or milliseconds. Sensitive marking limits some displays but does not encrypt the value in state or every output consumer. Flexible inputs make reuse possible, yet too many loosely validated options create difficult combinations. Expose meaningful decisions, keep safe defaults where appropriate, and avoid putting real secrets into a committed variable file merely because it is convenient.

Why this matters

A region variable has type string; a replicas variable has a validation requiring a positive integer.

A common trap

Do not overlook this operational distinction: Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment.

Read all topics in this lesson

A complete reference to revisit alongside the guided steps above.

Input variables and validation

Variables make configuration adjustable with explicit types, defaults, descriptions, and optional validation. Sensitive marking affects display but is not encryption. Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment. For example, a region variable has type string; a replicas variable has a validation requiring a positive integer.

WORKED EXAMPLE
A region variable has type string; a replicas variable has a validation requiring a positive integer.

A module accepts replicas as an integer and environment as a string. At plan time, callers supply values through reviewed files or flags. Type constraints reject incompatible shapes; validation can reject a numeric value outside the module's intended range. The resulting plan then shows how those accepted choices affect resources.

An input variable is a caller-supplied parameter. A default supplies a fallback when permitted. Validation expresses additional rules, such as requiring replicas greater than zero. Document units and meaning: a number called timeout is ambiguous without seconds or milliseconds. Sensitive marking limits some displays but does not encrypt the value in state or every output consumer. Flexible inputs make reuse possible, yet too many loosely validated options create difficult combinations. Expose meaningful decisions, keep safe defaults where appropriate, and avoid putting real secrets into a committed variable file merely because it is convenient.

Why this matters

A region variable has type string; a replicas variable has a validation requiring a positive integer.

A common trap

Do not overlook this operational distinction: Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment.

Local values

Locals name derived expressions within a module. They are not externally settable inputs and do not persist independently as remote resources. Use them for repeated naming, tags, and computations where a descriptive name improves readability without hiding meaningful resource relationships. For example, locals { app_name = "${var.environment}-orders" } derives one name consistently from an environment input.

WORKED EXAMPLE
locals { app_name = "${var.environment}-orders" } derives one name consistently from an environment input.

An environment input is prod and an application input is billing. A local expression combines them into prod-billing and another local builds a common tags map. Resources reuse those derived values. Changing the input produces consistent changes across references without a caller separately remembering to update each string.

A local value is an expression named within a module. A derived value is computed from other inputs. Scope determines where the name can be referenced. Locals are not independently configurable external parameters and do not create remote objects. Use them to explain repeated transformations, not to hide a long chain of opaque expressions. A local can depend on a resource attribute, making its value unknown until that attribute is available. Unknown values are a planning condition, not necessarily an error. A straightforward expression often improves readability more than several tiny aliases that force a reviewer to follow multiple files.

Why this matters

locals { app_name = "${var.environment}-orders" } derives one name consistently from an environment input.

A common trap

Do not overlook this operational distinction: Use them for repeated naming, tags, and computations where a descriptive name improves readability without hiding meaningful resource relationships.

Data sources and existing objects

Data sources read existing information through a provider instead of declaring ownership of a new managed resource. Reads can require permissions and may occur during planning or later if inputs are unknown. Avoid filters that could select an unintended object as the environment changes. For example, a data source looks up a known VPC by an explicit ID; a resource block would manage a VPC.

WORKED EXAMPLE
A data source looks up a known VPC by an explicit ID; a resource block would manage a VPC.

A team adopts an existing shared network without managing it in the application's state. A VPC data source reads the selected network's identifier and metadata, then application resources consume those results. The provider still needs permission to perform the lookup. If filters match an unintended network, the plan can attach resources to the wrong environment despite creating no network itself.

A data source reads information. A managed resource declares lifecycle ownership. A filter narrows a lookup's candidates. Prefer explicit stable selection when possible and verify account, region, tags, and uniqueness. A read may be deferred when its inputs are unknown during planning, so dependent values may remain unknown too. Reading shared infrastructure creates a dependency without transferring ownership. Coordinate changes with the actual owner; importing or recreating that shared object in the application state would introduce a different responsibility and possible conflict.

Why this matters

A data source looks up a known VPC by an explicit ID; a resource block would manage a VPC.

A common trap

Do not overlook this operational distinction: Avoid filters that could select an unintended object as the environment changes.

KEEP THESE WITH YOU

Today’s takeaways

Input variables and validation: Variables make configuration adjustable with explicit types, defaults, descriptions, and optional validation. Sensitive marking affects display but is not encryption. Values supplied by variable files or environment need deliberate precedence and protection, especially when automation selects an environment.

Local values: Locals name derived expressions within a module. They are not externally settable inputs and do not persist independently as remote resources. Use them for repeated naming, tags, and computations where a descriptive name improves readability without hiding meaningful resource relationships.

Data sources and existing objects: Data sources read existing information through a provider instead of declaring ownership of a new managed resource. Reads can require permissions and may occur during planning or later if inputs are unknown. Avoid filters that could select an unintended object as the environment changes.

CLOSE THE LOOP

Ready to complete this day?

Verify the practical steps and pass the knowledge check (at least two thirds correct).

Lab steps verifiedKnowledge check passed