How to Access Local Variables in Terraform Modules Using the terraform console Command

You can access root-module local variables directly as local.<name> in terraform console, but child-module locals must be exposed through outputs and referenced as module.<name>.<output>.

The terraform console command provides an interactive REPL for evaluating expressions against your current configuration in the hashicorp/terraform repository. Understanding how Terraform constructs the evaluation scope for this console is essential for debugging local values effectively.

Understanding the Terraform Console Evaluation Scope

When you launch terraform console, Terraform builds an evaluation scope that determines which values are accessible. This process is implemented in internal/command/console.go, where the command creates a lang.Scope populated by calling Eval on the configuration.

The scope includes:

  • All input variables (accessible as var.<name>)
  • All local values defined in the root module (accessible as local.<name>)
  • Any outputs from child modules (accessible as module.<MODULE>.<OUTPUT>)

According to the source code in internal/command/console.go, the scope initialization occurs around lines 31-38, where the console command sets up the evaluation context. Additionally, the console enables special helper functions by setting scope.ConsoleMode = true (lines 45-48), which activates console-only functions like tostring and jsonencode.

Accessing Root Module Local Variables

Local variables defined in your root module are immediately accessible in the console using the local.<name> syntax. These values are part of the public evaluation scope created when the console starts.

Consider a root module with the following configuration:


# main.tf

locals {
  instance_type = "t3.micro"
  environment   = "production"
}

You can inspect these values directly:

$ terraform console
> local.instance_type
"t3.micro"
> local.environment
"production"

The console evaluates these expressions using the graph nodes created by internal/terraform/transform_local.go, which implements the transformer responsible for processing local value blocks.

Accessing Child Module Local Variables

Local values defined within child modules are module-internal and are not exposed in the console's evaluation scope. Unlike root module locals, you cannot reference them using module.<NAME>.local.<NAME>.

To access a child module's local variables in the console, you must expose the value through an output in that child module, then reference it using module.<NAME>.<OUTPUT>.

Example: Exposing Child Module Locals

First, define the local value in your child module:


# modules/network/locals.tf

locals {
  subnet_cidr = "10.0.1.0/24"
  port        = 8080
}

Then expose it via an output:


# modules/network/outputs.tf

output "subnet_cidr" {
  value = local.subnet_cidr
}

output "application_port" {
  value = local.port
}

Reference the outputs from the root module console:

$ terraform console
> module.network.subnet_cidr
"10.0.1.0/24"
> module.network.application_port
8080

Console-Only Functions and Features

The terraform console provides helper functions that are only available in interactive mode. As implemented in internal/command/console.go, setting scope.ConsoleMode = true enables additional evaluation capabilities useful for debugging.

Common console-only operations include:

$ terraform console
> jsonencode({ name = local.instance_type, env = local.environment })
"{\"name\":\"t3.micro\",\"env\":\"production\"}"

> tostring(42)
"42"

These functions allow you to format and inspect complex data structures during interactive debugging sessions.

Prerequisites and Workflow

Before using terraform console to inspect local variables, ensure your working directory is properly initialized.

  1. Initialize the directory to download providers and modules:
terraform init
  1. Launch the console:
terraform console
  1. Evaluate expressions using local.<name> for root module values or module.<NAME>.<OUTPUT> for child module data.

If you attempt to access a child module's local directly without an output, Terraform will return an error indicating that the reference is invalid.

Summary

  • Root module local variables are accessible in terraform console using the local.<name> syntax.
  • Child module locals are module-internal and cannot be accessed directly; expose them through outputs and reference as module.<name>.<output>.
  • The evaluation scope is constructed in internal/command/console.go, which populates the lang.Scope with root module locals and enables console-specific functions via scope.ConsoleMode = true.
  • Always run terraform init before using the console to ensure providers and modules are available.
  • Local value graph nodes are created by internal/terraform/transform_local.go, which processes local blocks during the evaluation phase.

Frequently Asked Questions

Can I access child module locals directly in terraform console?

No, child module locals are intentionally module-internal and are not part of the public address space. According to the implementation in hashicorp/terraform, the console's evaluation scope only includes root module locals. To access child module data, you must expose the value through an output in the child module and reference it using module.<name>.<output>.

Why do I need terraform init before using terraform console?

The terraform init command downloads required providers and modules, builds the dependency graph, and prepares the evaluation context. Without initialization, the console cannot construct the lang.Scope needed to resolve local variables or module references, as the configuration tree and provider schemas are not yet loaded.

What functions are available only in terraform console?

Console-only functions include formatting helpers like jsonencode, tostring, and other debugging utilities. These are activated when internal/command/console.go sets scope.ConsoleMode = true during scope creation. These functions are specifically designed for interactive inspection and are not available during standard plan or apply operations.

How do I debug local variable values in nested modules?

For nested or child modules, add an output block that exposes the local value you need to inspect. In the child module, create an output referencing local.<name>, then run terraform console from the root module and query the value using module.<module_name>.<output_name>. This is the only supported method for inspecting module-internal local values.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →