TExcelTAD

Outputs & Locals

Variables are a configuration's inputs. Outputs are its results: values a person, a script, a pipeline or another module reads after apply. Locals are named intermediate values inside the configuration, like local variables in a function.

Locals: name things once

hcl
locals {
  name_prefix = "${var.project}-${var.environment}"          # linkly-staging
  common_tags = {
    Project     = var.project
    Environment = var.environment
    ManagedBy   = "opentofu"
  }
}

resource "aws_s3_bucket" "assets" {
  bucket = "${local.name_prefix}-assets"
  tags   = merge(local.common_tags, { Purpose = "static assets" })
}

Use a local when an expression is repeated or when a name makes the intent clearer. Don't turn every value into a local: a reader has to jump to find it. Unlike variables, locals cannot be set from outside.

Outputs

bash · try it yourself
mkdir o && cd o
cat > main.tf <<'HCL'
variable "project" {
  type    = string
  default = "linkly"
}
variable "environment" {
  type    = string
  default = "staging"
}

locals {
  name_prefix = "${var.project}-${var.environment}"
  common_tags = {
    Project     = var.project
    Environment = var.environment
    ManagedBy   = "opentofu"
  }
  ports = { http = 80, https = 443 }
}

resource "local_file" "inventory" {
  filename = "${local.name_prefix}.ini"
  content  = "[web]\n${local.name_prefix}-web-1\n"
}

output "inventory_file" {
  description = "Path of the generated inventory."
  value       = local_file.inventory.filename
}

output "tags" {
  value = merge(local.common_tags, { Purpose = "demo" })
}

output "ports" {
  value = local.ports
}
HCL
tofu init >/dev/null
tofu apply -auto-approve | sed -n '/^Outputs:/,$p'
echo "--- one output, raw (no quotes): for scripts"
tofu output -raw inventory_file; echo
echo "--- all outputs as JSON: for tools"
tofu output -json | jq -c '{file: .inventory_file.value, https: .ports.value.https, sensitive: .tags.sensitive}'
Outputs:

inventory_file = "linkly-staging.ini"
ports = {
  "http" = 80
  "https" = 443
}
tags = {
  "Environment" = "staging"
  "ManagedBy" = "opentofu"
  "Project" = "linkly"
  "Purpose" = "demo"
}
--- one output, raw (no quotes): for scripts
linkly-staging.ini
--- all outputs as JSON: for tools
{"file":"linkly-staging.ini","https":443,"sensitive":false}
Command Gives
tofu output every output in HCL style
tofu output name one output in HCL style (strings quoted)
tofu output -raw name a string or number with no quotes or newline, for $(…) in shell
tofu output -json all outputs with value, type and sensitive, for jq

Outputs are read from the state, not recomputed: they show the values from the last apply, even if the code changed since.

Who reads outputs

  • People: the URL of the new load balancer, the bastion's IP.
  • Pipelines: a deploy job reads the ECR repository URL or cluster name with tofu output -raw.
  • Parent modules: a module's outputs are the only values its caller can use: module.network.private_subnet_ids (Modules).
  • Other configurations: the terraform_remote_state data source reads another state's outputs. It couples the two configurations tightly and needs read access to the whole state; many teams prefer publishing the values to SSM Parameter Store and reading them with a data source.

An output can carry sensitive = true (shown as <sensitive>, still readable with -raw/-json), a precondition that fails the apply when a promise isn't kept, and depends_on in rare ordering cases:

hcl
output "api_url" {
  value = "https://${aws_lb.api.dns_name}"
  precondition {
    condition     = aws_lb.api.internal == false
    error_message = "The API load balancer must be internet-facing."
  }
}

Tip: Treat a root module's outputs as an API. Removing or renaming one breaks every script and pipeline that reads it, so change them as carefully as a function signature.

Test yourself

Which command prints an output without quotes, for use in a shell variable?

  1. tofu output name
  2. tofu output -raw name
  3. tofu show name
  4. tofu output -plain name

Can you override a local value from the command line?

  1. Yes, with -var
  2. Yes, with TF_VAR_
  3. No: locals are internal; make it a variable if it must be settable
  4. Only in tfvars files

You change an output's expression but don't apply. What does tofu output show?

  1. The new value
  2. The value from the last apply, stored in state
  3. An error
  4. Nothing