पाठ 6 / 25

Locals and Outputs

Name intermediate values; expose results.

locals for reuse, outputs for consumers

Locals give names to expressions used several times (local.app_name, a common tags map) to avoid repetition. Outputs expose values after apply: to the user (terraform output), to calling modules (module.x.output_name) or to other tooling. Keep outputs meaningful (ids, endpoints, names) and mark outputs derived from secrets as sensitive.

Apply, read the file, plan again, run

I ran this with Terraform 1.16.4 and the hashicorp/local 2.9.1 and hashicorp/random 3.9.1 providers, which manage local files and random values, so no cloud account was needed; each example starts from a fresh directory. Apply creates the file with content built from locals and prints the output; a second plan reports no changes, because the real resource already matches the configuration (idempotence).

terraform apply -auto-approve -no-color | tail -4
cat out/shop-dev.conf
terraform plan -no-color | grep -E "No changes|Plan:"

Output:


Outputs:

config_path = "./out/shop-dev.conf"
app=shop
env=dev
No changes. Your infrastructure matches the configuration.

Use a common tags local

Define locals { tags = { app = "shop", env = var.env } } and apply it to every resource for cost tracking and ownership.

त्वरित जाँच: Why did the second plan show no changes?

  • Outputs block changes
  • The plan failed
  • Terraform forgot the resource
  • Real infrastructure already matched the configuration
Answer

Real infrastructure already matched the configuration — Applying the same config twice is idempotent.