terraform console is an interactive REPL that evaluates HCL expressions on the fly. You can verify expression correctness, type conversions, and loop results before plan/apply, dramatically speeding up your validation loop.
This article walks through the expression-evaluation basics frequently tested on the Associate and Pro exams, plus debugging locals/variables/state references and reading errors — practical workflows you can apply directly.
terraform console loads the configuration and state from the current working directory and evaluates HCL expressions one by one. If state exists, you can reference resource attributes and module outputs.
It is safest to run terraform init first so that backends and providers are resolved. With a remote backend configured and valid credentials, you can evaluate against that state as well. Exit with exit or Ctrl-D.
| Tool | Primary use | What you get | Interactivity |
|---|---|---|---|
| terraform console | Arbitrary expression evaluation and debugging | Expression results, attributes/outputs in state | High (interactive) |
| terraform output | Inspect output variables | Output values of the root module only | Low (fixed display) |
| terraform plan -json | Machine-readable diff output | Planned diffs and visibility of unknown values | Low (non-interactive, for analysis) |
Expression evaluation flow (conceptual)
Input expr --> HCL parser --> Evaluator
| \-- Function calls
| \-- Variable/locals references
| \-- State references (resource/module)
V
Result / ErrorStartup and basic evaluation examples
# Variables resolve from TF_VAR_* or tfvars; state is tied to the working directory
$ TF_VAR_env=dev terraform console
> terraform.workspace
"default"
> var.env
"dev"
> tostring(1 + 2)
"3"
> can(regex("^app-", "app-web"))
true
> coalesce(null, "fallback")
"fallback"
> exitTerraform's primitive types are string, number, bool, collections (list/tuple, map/object), and null. In the console, explicitly shaping types with functions reduces errors.
Expressions containing unknown values (unapplied, undetermined attributes) or null can raise evaluation errors. Check evaluability up front with can(), and prepare fallbacks with try() or coalesce* to make debugging easier.
Type handling and safe-evaluation examples
terraform console
> toset(["a", "b", "a"]) # deduplicate
set([
"a",
"b",
])
> tomap({ a = 1, b = 2 })
{
"a" = 1
"b" = 2
}
> can(var.unknown)
false
> try(var.missing, "ok")
"ok"
> coalescelist([], [1,2])
[
1,
2,
]
> tonumber("08")
8The console resolves the locals and variables in the current configuration. Reference locals as local.<name>. Provide tfvars or TF_VAR_* env vars so you can try expressions against production-like inputs.
When state exists, you can also evaluate resource addresses (e.g. aws_instance.web.id) and module outputs (module.app.url). Unapplied attributes are unevaluable, so guard with try() and validate fallback logic.
Iterate between configuration and the console
# locals.tf (example)
locals {
tags = merge({ app = "sample" }, { env = var.env })
}
# variables.tf
variable "env" { type = string, default = "dev" }
# Evaluate in the console
$ terraform console
> local.tags
{
"app" = "sample"
"env" = "dev"
}
# Only available when present in state (errors otherwise)
> try(aws_instance.web.private_ip, "no-ip")
"no-ip"
> can(module.app.url)
falseFor-expressions and conditional comprehensions are easiest to finalize when you build them in the console before plugging them into resource arguments. Transform keys/values of maps/objects and use flatten / distinct to meet real-world shaping requirements.
Combine regex, branching, and sorting and iterate until the result matches your target schema.
For-expression and shaping examples
terraform console
> xs = [{name="a", port=80}, {name="b", port=443}]
> [for x in xs : format("%s:%d", x.name, x.port)]
[
"a:80",
"b:443",
]
> m = { web = {subnets=["s-1","s-2"]}, api = {subnets=["s-3"]} }
> flatten([for k, v in m : v.subnets])
[
"s-1",
"s-2",
"s-3",
]
> addrs = ["10.0.0.1", "10.0.0.1", "10.0.0.5"]
> sort(distinct(addrs))
[
"10.0.0.1",
"10.0.0.5",
]Errors in the console can also surface during real plan/apply runs, so classify the cause precisely. The main culprits are undefined (unknown/unapplied) values, type mismatches, missing keys, and bad file paths.
Isolate with can()/try() and evaluate small sub-expressions in pieces to reach the root cause faster.
Examples of error isolation
terraform console
# Undefined reference
> aws_instance.web.id
.
| Error: Invalid reference
| on <console-input> line 1:
| (resource not found in state)
.
# Guard with can / try
> can(aws_instance.web.id)
false
> try(aws_instance.web.id, "no-id")
"no-id"
# Fixing a type mismatch
> merge({a=1}, {b=null})
{
"a" = 1
"b" = null
}
> tomap(merge({a=1}, {b=null})) # force map type
{
"a" = 1
"b" = null
}
# File existence check
> fileexists("./vars/dev.tfvars")
trueThe Associate/Pro exams frequently cover console-driven expression validation, implicit vs explicit type conversion, for-expressions with conditions, handling unknown values, and workspace branching. Questions tend to probe behavioral accuracy (what can be referenced) and the correct application of safe guards (try / can / coalesce).
In practice, it pays to lock down expressions in the console before plan, prototype state-driven data shaping in the console first, and prepare production-equivalent inputs via tfvars and TF_VAR_* to verify behavior.
Selected tricks
# Evaluate against a different state file (already downloaded locally)
$ terraform console -state=./terraform.tfstate
> module.app.endpoint
"https://app.example.com"
# Verify workspace branching
$ TF_VAR_env=prod terraform console
> terraform.workspace
"default"
> var.env
"prod"
> var.env == "prod" ? "hoge" : "fuga"
"hoge"
# Validate file input
> jsondecode(file("./overrides.json"))
{
"image" = "1.2.3"
}Associate / Pro
問題 1
On a project whose state is managed in a remote backend (e.g. Terraform Cloud), you want to evaluate module.web's url output via terraform console. Which approach is correct?
正解: A
The console uses the current configuration/backend to load state (assuming init has run and credentials are valid). Module outputs are stored in state, so if they have been applied, the console can read them. terraform output only displays output values and cannot evaluate arbitrary expressions.
How do I pass variable values into terraform console?
Variable values come from the variable's default, *.auto.tfvars / terraform.tfvars files, or the TF_VAR_<name> environment variable. For example: TF_VAR_region=ap-northeast-1 terraform console. These let you reproduce production-like inputs.
Can I evaluate unapplied resource attributes in the console?
Attributes that do not exist in state cannot be evaluated. Guard with try() or can() and validate the shape of expressions using placeholder values. Confirm final values after plan/apply.
Can I control which state file is read?
By default, state is read from the backend defined in the working directory's configuration. To target a specific local state, use the -state option to specify the path (run terraform init first to be safe).
Practice with certification-focused question sets
Try free practice questionsNicheeLab Editorial Team
NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.
HCL Syntax: Terraform's Configuration Language (2026)
HCL2 fundamentals for Terraform — blocks, attributes, expres...
Terraform Authoring & Operations Pro: Complete Guide (2026)
Tactics for the Terraform Pro exam — module authoring, works...
Terraform Providers: Plugin Management Fundamentals (2026)
Provider mechanics — required_providers, versions, mirrors, ...
Terraform Resource Blocks: Declarative Infra Units (2026)
Resource block fundamentals — addresses, references, common ...
Terraform Data Sources: Read-Only External Data (2026)
Data source basics — declaration, refresh behavior, dependen...