Infrastructure Lifecycle and Operations · Topic 4 of 5
Project Data and Version Management
Terraform Professional 002: pass data with variables, outputs, data sources and remote state, then control provider and module versions with the lock file.
- 5 min
terraform_remote_statereads only root outputs but needs access to the full snapshot.- Re-export child module outputs from the root to expose them.
-jsonand-rawprint sensitive values in plain text.
What it is
Every project takes data in and publishes data out, through input variables, data sources, outputs and shared state. As projects split, choosing the right channel decides how much access each team needs.
Version management decides which provider and module releases a run uses. Most questions ask you to predict what terraform init will select.
How it works
Getting data in
| Channel | Use it for |
|---|---|
| Input variable | Values the caller decides |
| Data source | Reading objects that exist outside this configuration |
| Module output | Values from a child module |
terraform_remote_state | Root outputs of another state |
tfe_outputs | Workspace outputs in HCP Terraform or Terraform Enterprise |
Data sources are normally read during planning. If an argument depends on a value not known until apply, the read is deferred and dependent attributes show as (known after apply).
Sharing between projects
terraform_remote_state exposes only root module outputs, but its users need access to the entire state snapshot, which can hold sensitive values. For that reason the docs prefer publishing data elsewhere.
On HCP Terraform, use tfe_outputs, which does not need full state access. Elsewhere, pair a managed resource with its data source, such as an SSM parameter written by one project and read by another.
Getting data out
output "subnet_ids" {
value = [for s in aws_subnet.app : s.id]
}
output "db_endpoint" {
value = module.database.endpoint
sensitive = true
}
Only root module outputs reach the CLI and other projects, so a child module's output must be re-exported as db_endpoint is above.
| Command | Result |
|---|---|
terraform output | Lists outputs; sensitive ones show as <sensitive> |
terraform output db_endpoint | Shows that value unredacted |
terraform output -json | All outputs as JSON, sensitive values in plain text |
terraform output -raw NAME | Plain string, number or boolean; no complex types |
sensitive = true only hides the value in CLI output. It is still in state, so anyone who can read state can read it.
Conditional resources and references
resource "aws_eip" "nat" {
count = var.enable_nat ? 1 : 0
}
output "nat_ip" {
value = one(aws_eip.nat[*].public_ip)
}
Trace it: when enable_nat is true, the splat gives a one-item list and one returns that item. When it is false, the list is empty and one returns null, so the output is simply absent rather than an error.
Version constraints
| Constraint | Allows |
|---|---|
= 1.4.0 or 1.4.0 | Exactly that version |
>= 5.0 | 5.0 and anything newer |
~> 5.30.0 | 5.30.1 and 5.30.9, but not 5.31.0 |
~> 5.30 | 5.31 and 5.99, but not 6.0 |
Terraform must satisfy all constraints from root and child modules together. Reusable modules should set a minimum version only, while root modules should use ~> for each provider. Pin third-party modules exactly.
The lock file
.terraform.lock.hcl records provider selections and checksums, and it belongs in version control. It covers providers only: modules are not recorded, so the constraint alone decides the module version.
Trace a provider upgrade with version = "~> 5.30.0", a lock at 5.30.2, and newer releases 5.30.5 and 5.31.0:
terraform initkeeps 5.30.2, because the lock wins while it satisfies the constraint.terraform init -upgradeselects 5.30.5, the newest allowed.- Editing the constraint to
~> 5.31.0makes plain init fail withdoes not match configured version constraint. terraform init -upgradethen selects 5.31.0 and updates the lock.
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64
terraform init -lockfile=readonly
The first command records checksums for every platform your team uses. The second verifies checksums and refuses to change the lock, which suits pipelines. It cannot be combined with -upgrade.
Upgrading a pinned module
With version = "2.3.0", a new release is never picked up on its own. Change the version argument, then run terraform init -upgrade. Running -upgrade without editing the pin, or deleting the lock file, gains nothing.
How the exam asks about this
- "Another team needs your VPC ID in HCP Terraform but should not read your whole state." Use tfe_outputs.
- "A new provider release exists, but init keeps installing the old one." The lock file holds the selection; run
terraform init -upgrade. - "A pipeline must parse every output into a script." Use terraform output -json, and treat its logs as sensitive.
What to remember for the exam
terraform_remote_statereads only root outputs but needs access to the full snapshot.- Re-export child module outputs from the root to expose them.
-jsonand-rawprint sensitive values in plain text.~>lets only the rightmost stated component increase.- The lock file covers providers, not modules, and should be committed.
required_versionconstrains the CLI only, not providers.
The version argument in a provider block is deprecated and only produces a warning. Set provider constraints in required_providers inside the terraform block.
Common mistakes
Adding the lock file to .gitignore. Each machine and pipeline may then select different provider releases. Commit it so upgrades go through review.
Assuming sensitive means encrypted. The flag only redacts CLI display. Protect the state itself with access control and encryption at rest.
Expecting version on a local module. A module sourced from ./modules/web has no version to pin. Use a registry source if you need versioned releases.
Check yourself
Why might you choose tfe_outputs over terraform_remote_state?
tfe_outputsreads workspace outputs without full access to the state, whileterraform_remote_staterequires access to the entire snapshot.Does .terraform.lock.hcl pin module versions?
No. It records providers only, so a module's version comes from its constraint, and an exact version pins it.
What does one(aws_eip.nat[*].public_ip) return when count is 0?
It returns
null, becauseonereturns null for an empty list.
Topic 4 of 5 in Infrastructure Lifecycle and Operations
Test yourself on Infrastructure Lifecycle and Operations
The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.