Outputs and locals

Beginner · 7 min

Use locals to name a value once and reuse it, and outputs to publish results to the CLI, to a parent module and to scripts that need an address or an ID.

Before thisInput variables and validation

Tech Tutorials with Piyush · 20 min · from 13:24

Outputs and locals

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Tech Tutorials with PiyushReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A `locals` block names an expression so you write it once. Locals are private to the module and are read as `local.name`.
  • Locals can refer to each other and to resources. Terraform sorts them by those references and rejects a cycle.
  • An `output` block publishes a value after apply. At the root it prints to the CLI and can be read by scripts, and in a module it is how the caller gets a value back.
  • `terraform output -raw name` prints one plain value for a script. `-json` prints everything, with sensitive values included.

Two small blocks that remove a lot of mess

Look at an unrefactored config for a service. The string "acme-${var.environment}" appears in the bucket name, the role name, the log group and four tags. Then the company is renamed. Or a script that deploys the app needs the address of the load balancer that Terraform made, and the only way to get it is to open the console and copy it.

Locals fix the first problem and outputs fix the second. They are the least dramatic blocks in the language and you will use them in every project.

Locals: names for expressions

A locals block assigns names to expressions. Written from the Terraform docs. I did not run it:

locals {
  name_prefix = "${var.project}-${var.environment}"
  common_tags = {
    project     = var.project
    environment = var.environment
    managed_by  = "terraform"
  }
}

resource "aws_s3_bucket" "logs" {
  bucket = "${local.name_prefix}-logs"
  tags   = local.common_tags
}

You read them as local.name_prefix, singular, though the block is locals. They are not inputs, since nobody outside can set them, and not outputs, since nothing outside can see them. They are private names inside the module.

Use a local when an expression repeats, or when naming it makes the code read better. Do not use one for every literal. local.five = 5 is just indirection.

Locals can refer to each other and to resources, and Terraform orders them by those references, as it does with everything. To check how that ordering and cycle detection works, I wrote a small stand-in that finds local.x references and sorts them:

['env', 'prefix', 'bucket']
ERROR cycle: a -> b -> a

The first line is a valid order for three locals where each uses the one before. The second is two locals that need each other, which has no valid order. Terraform’s own message differs, but the rule is the same: the dependency graph from the earlier lesson covers locals too.

Outputs: values that leave

An output publishes a value:

output "logs_bucket_name" {
  value       = aws_s3_bucket.logs.bucket
  description = "Where access logs land."
}

output "db_password" {
  value     = random_password.db.result
  sensitive = true
}

Outputs serve three audiences:

  1. You, in the terminal. After apply, root outputs are printed. terraform output shows them again later, from state, without running anything.
  2. Scripts and pipelines. terraform output -raw logs_bucket_name prints one value with no quotes, which a shell can capture. terraform output -json prints all of them as JSON for tools like jq.
  3. The caller of a module. Inside a child module, an output is the only way to hand a value to its parent. A VPC module outputs vpc_id, and the root passes module.network.vpc_id into something else. Modules get their own group, starting with what modules are.

A sensitive output is shown as <sensitive> in the CLI. Treat it as a courtesy. The CLI will still print it if you ask for it explicitly with -raw or -json, and it is stored in state in plain text.

What goes in an output

Publish what someone downstream will need: IDs, ARNs, endpoints and names. Skip the rest. The popular AWS VPC module publishes a lot more than most. I asked the registry how many:

$ curl -s https://registry.terraform.io/v1/modules/terraform-aws-modules/vpc/aws | jq -c '{version, outputs: (.root.outputs|length)}'
{"version":"6.9.0","outputs":119}

A general-purpose public module can justify that many. A module you write for your team probably cannot, and every output is a promise you must keep when you rename a resource.

Add a description to every output. It is documentation that travels with the code.

A common trap

An output that refers to a value which is unknown until apply, such as a generated ID, is fine. The plan will say (known after apply). But an output is not a way to make one resource wait for another. If two resources need an ordering and no reference between them, express it with a reference or depends_on rather than trying to route it through an output. Dependencies is a lesson later in this group.

About the video

The embed is the last two chapters of Tech Tutorials with Piyush’s “Day 5/28 - Terraform Variables - Input vs Output vs Local Variables”, January 2025: “Output variables” and “Local variables” (13:24 to the end, about 7 minutes). The earlier part of the same video is the clip in the previous lesson. I judged the content from the chapter list. The presenter uses AWS examples. English was judged from title, channel and English auto-captions.

Where this fits

Variables so far are strings and numbers. Real modules pass lists, maps and objects, and the type system decides what is accepted: type constraints and complex variables.

Check yourself

  1. 01You repeat `"${var.project}-${var.environment}"` in six places. What do you change?

    Add `locals { name_prefix = "${var.project}-${var.environment}" }` and use `local.name_prefix`. A rename becomes a one-line edit.

  2. 02How is an output different from an input variable?

    An input is set by whoever runs the configuration or calls the module. An output goes the other way: the configuration reports a computed value back to the CLI or to the module's caller.

  3. 03Two locals refer to each other. What does Terraform do?

    It reports a cycle error. Locals form a dependency graph like resources do, so mutual references have no valid order.

  4. 04A shell script needs the load balancer address after apply. Which command gives it cleanly?

    `terraform output -raw lb_dns_name`, which prints just the value with no quotes or formatting, so the script can capture it directly.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark