linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
Intro
Lessons
Footer
linuxlab-TutorialsPricingAboutPrivacy & cookies
Copyright © 2026 LinuxLab. All rights reserved.
linuxlab.io
Tutorials▾
  • Linux & networking
    File system, processes, TCP/IP, BGP and OSPF
    →
  • Terraform & IaC
    HCL, state, plan/apply on a LocalStack sandbox
    →
  • Git & GitHub
    Object model, plumbing, branching, GitHub Actions
    →
All tutorials →
PricingAboutSign inCreate account
/
  • Introduction
  • Lessons
  • How it works
  • Knowledge base
  • Cheat sheet
  • Capstone
  • Interview prep
home/terraform/kb/Workflow/tf-console

kb/workflow ── Workflow ── beginner

terraform console: REPL for HCL expressions

`terraform console` is an interactive REPL: you type an HCL expression and get the evaluated value back. You can test functions (`upper("foo")`), inspect types (`type(var.x)`), and read state (`aws_s3_bucket.demo.arn`). It changes nothing and only reads.

view as markdownaka: terraform-console, hcl-repl

What console does

terraform console is a REPL (Read-Eval-Print Loop). You type an expression, Terraform evaluates it, and prints the result. It works like python or node with no flags.

This is the most underrated tool in Terraform. It is free, does not modify state, and does not call the cloud (beyond reading already-refreshed data), yet it saves hours of debugging.

Basic usage

bash
cd ~/myproject
terraform console

The prompt appears:

>

Type expressions:

> 1 + 2
3
> "hello, " + "world"
"hello, world"
> upper("terraform")
"TERRAFORM"
> length([1, 2, 3, 4])
4

To exit, type exit or press Ctrl+D.

Checking variables and locals

> var.region
"us-east-1"
> local.name_prefix
"linuxlab-dev-us-east-1"
> local.common_tags
tomap({
  "Env" = "dev"
  "ManagedBy" = "terraform"
  "Project" = "linuxlab-terraform-course"
})

This is the main use case. When a locals value is assembled from five nested functions, reading it by eye is not feasible. In console, one expression shows the actual computed value.

Checking types

The type() function is a special operator available only in console:

> type(var.region)
string
> type(var.tags)
map of string
> type([1, 2, 3])
tuple([number, number, number])
> type(toset(["a", "b"]))
set of string

This is useful when you are unsure whether something is a list or a tuple, a map or an object. See hcl-types for the difference.

Accessing state

After apply, all created resources are readable in console:

> aws_s3_bucket.demo.arn
"arn:aws:s3:::linuxlab-hello-abc123"
> aws_s3_bucket.demo.tags
tomap({
  "Owner" = "student"
})
> [for b in aws_s3_bucket.many : b.id]
[
  "linuxlab-count-0-abc",
  "linuxlab-count-1-def",
]

This is safe. No changes happen. It is plain state reading through the same expressions you use in HCL.

Testing functions

Before writing an expression in HCL, test it in console:

> format("%s in region %s", upper("dev"), "us-east-1")
"DEV in region us-east-1"
> merge({a = 1, b = 2}, {b = 99, c = 3})
{
  "a" = 1
  "b" = 99
  "c" = 3
}
> [for x in [1, 2, 3, 4] : x * 2 if x > 2]
[
  6,
  8,
]

This is much faster than the cycle of editing HCL, running plan, reading the diff, and realizing the expression was wrong. Console is a unit test for a single line.

Non-interactive mode with echo

Sometimes you need a single expression value from a script:

bash
echo 'type(var.tags)' | terraform console
echo 'aws_s3_bucket.demo.arn' | terraform console -no-color

Pass it via stdin. Convenient for CI or documentation.

Multi-line expressions

In the REPL you can enter multiple lines by wrapping them in braces:

> {
  name = "myapp"
  tags = {
    Owner = "platform"
  }
}
{
  "name" = "myapp"
  "tags" = {
    "Owner" = "platform"
  }
}

This is useful for testing complex object literals.

Pitfalls

  • Console requires a valid config. If HCL has a syntax error, console will not start. Run validate first.

  • Console reads the current state. If state is empty (before apply), aws_s3_bucket.demo.arn will fail with "resource not found". That is expected: the resource does not exist yet.

  • Console does not refresh. Values come from the state file as it stands. If something changed in the cloud outside Terraform, console will not see it. To update: terraform refresh (but it modifies state, use with care).

  • Sensitive values are masked. If an output is marked sensitive = true, console shows (sensitive value). To reveal it, use the nonsensitive() function.

  • Workspace matters. Console operates in the active workspace. If you switched to staging, you see the staging state. terraform workspace show prints the current workspace.

  • Console does not run provisioners. No local-exec, no external commands. Only pure expressions.

§ команды

bash
terraform console

Start the interactive REPL. Press Ctrl+D or type exit to quit.

bash
echo 'type(var.tags)' | terraform console

Non-interactive: get the type of a single variable. Useful in scripts.

bash
echo 'length(aws_s3_bucket.many)' | terraform console

Count how many resources were created with count or for_each, without entering the state list.

bash
terraform console -no-color

Disable ANSI colors. Use in scripts and logs that do not parse escape codes.

§ см. также

  • hcl-syntaxHCL: the language you write Terraform inHCL (HashiCorp Configuration Language) is the language you use to describe the desired state of your infrastructure. It looks like JSON, but it is easier to read: you can write comments, variables, and loops.
  • hcl-typesData types in HCL: string, number, list, map, objectHCL supports primitives (string, number, bool) and complex types: list, set, map, tuple, object. This article covers the syntax of each one and the difference between the look-alikes (list vs tuple, map vs object).
  • tf-functions-stringHCL string functions: format, join, replace, lower, and moreHCL string functions: format (like printf), join/split (lists to strings and back), replace (regex or text), lower/upper/title (case), trimspace and the trim* family. All pure, no side effects, handy in locals.
  • tf-functions-collectionHCL collection functions: length, lookup, merge, concat, flattenHCL has functions for list/set/map: length (size), lookup (by key with a default), merge (combine maps), concat (join lists), flatten (unnest), keys/values. The basic toolkit for transformations.
  • tf-referencesReferences in HCL: how to read aws_s3_bucket.demo.bucketAny value in HCL is reachable through an address: var.x, local.x, aws_s3_bucket.demo.arn, module.net.vpc_id, data.aws_region.current.name. Knowing this syntax is half of your productivity in Terraform.
Footer
linuxlab-
Copyright © 2026 LinuxLab. All rights reserved.
Tutorials
Pricing
About
Privacy & cookies