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
In a nutshell
  • terraform_remote_state reads only root outputs but needs access to the full snapshot.
  • Re-export child module outputs from the root to expose them.
  • -json and -raw print 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.

Variables and data sourcesProjectOutputsOther projectstfe outputs or remote stateVersion constraintsterraform initLock fileprovider selections

How it works

Getting data in

ChannelUse it for
Input variableValues the caller decides
Data sourceReading objects that exist outside this configuration
Module outputValues from a child module
terraform_remote_stateRoot outputs of another state
tfe_outputsWorkspace 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.

CommandResult
terraform outputLists outputs; sensitive ones show as <sensitive>
terraform output db_endpointShows that value unredacted
terraform output -jsonAll outputs as JSON, sensitive values in plain text
terraform output -raw NAMEPlain 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

ConstraintAllows
= 1.4.0 or 1.4.0Exactly that version
>= 5.05.0 and anything newer
~> 5.30.05.30.1 and 5.30.9, but not 5.31.0
~> 5.305.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:

  1. terraform init keeps 5.30.2, because the lock wins while it satisfies the constraint.
  2. terraform init -upgrade selects 5.30.5, the newest allowed.
  3. Editing the constraint to ~> 5.31.0 makes plain init fail with does not match configured version constraint.
  4. terraform init -upgrade then 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_state reads only root outputs but needs access to the full snapshot.
  • Re-export child module outputs from the root to expose them.
  • -json and -raw print sensitive values in plain text.
  • ~> lets only the rightmost stated component increase.
  • The lock file covers providers, not modules, and should be committed.
  • required_version constrains the CLI only, not providers.
Watch out

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_outputs reads workspace outputs without full access to the state, while terraform_remote_state requires 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, because one returns 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.