Have you ever needed a quick and easy way to experiment with Terraform functions and expressions? In this article, we will explore the terraform console command and provide some examples of how to use it.
The Terraform HCL language includes a number of built-in functions you can use in expressions.
Expressions refer to or compute values within a configuration. The simplest expressions are literal values, like "hello" or 5, but the Terraform language also allows more complex expressions such as references to data exported by resources, arithmetic, conditional evaluation, and a number of built-in functions.
Note: Terraform does not support user-defined or ‘custom’ functions.
What is the Terraform console command?
The terraform console command is a part of the Terraform CLI that opens an interactive console where you can experiment with testing interpolations before using them in configurations and interact with any values currently saved in the state.
This command’s primary purpose is to help you understand how Terraform evaluates expressions and functions based on your configuration, making it very useful for testing and debugging.
How does terraform console work?
You launch it by running terraform console in a working directory, it loads the existing state file and configuration, giving access to resource attributes, input variables, locals, outputs, and built-in functions. Let’s see some examples.
Testing simple expressions and functions
Enter the Terraform console command:
terraform console
We will first use the split expression to test. At the console > prompt, type:
split(",", "follow,this,blog")
The output shows how the split command will split the string you provided with the , separator into a list:
Try lower("TEST")
As you can see, you can use the terraform console to test functions and expressions to test the output before modifying your configuration files.
You can also pipe a command directly to the terraform console:
echo 'lower("TEST")' | terraform consoleTesting variables, locals, and outputs
To test local variables inside the Terraform console, you should have a local variable already defined inside your Terraform configuration.
locals {
instance_names = ["dev1", "dev2", "qa1", "qa2", "qa3", "prod1", "prod2"]
}You can access the variable in the Terraform console by prefixing it with local.
> local.instance_names
[
"dev1",
"dev2",
"qa1",
"qa2",
"qa3",
"prod1",
"prod2",
]Functions can also be leveraged directly on your local variables there:
> length(local.instance_names)
7
> element(local.instance_names, 4)
"qa3"
> formatlist("Instance %s is running", local.instance_names)
tolist([
"Instance dev1 is running",
"Instance dev2 is running",
"Instance qa1 is running",
"Instance qa2 is running",
"Instance qa3 is running",
"Instance prod1 is running",
"Instance prod2 is running",
])You can even use loops and conditional statements directly on your local variable:
> [for instance in local.instance_names: instance if startswith(instance, "qa")]
[
"qa1",
"qa2",
"qa3",
]Testing for_each loops
You cannot directly test the result of a for_each loop inside of the Terraform console. However, you can achieve the same behavior by leveraging a for loop. Let’s suppose you have the following local variable:
instances = {
dev1 = {
instance_type = "t2.micro"
ami_id = "dev1ami"
}
dev2 = {
instance_type = "t3.micro"
ami_id = "dev2ami"
}
dev3 = {
instance_type = "t3.micro"
ami_id = "dev3ami"
}
}Now, let’s replicate the behavior of for_each in the console.
> { for key, value in local.instances : key => value }
{
"dev1" = {
"ami_id" = "dev1ami"
"instance_type" = "t2.micro"
}
"dev2" = {
"ami_id" = "dev2ami"
"instance_type" = "t3.micro"
}
"dev3" = {
"ami_id" = "dev3ami"
"instance_type" = "t3.micro"
}
}Now let’s take it up a notch to understand who each.key and who is each.value:
> [for key, value in local.instances: format("Each.key is %s. Each.value is %v", key, value)]
[
"Each.key is dev1. Each.value is {\"ami_id\":\"dev1ami\",\"instance_type\":\"t2.micro\"}",
"Each.key is dev2. Each.value is {\"ami_id\":\"dev2ami\",\"instance_type\":\"t3.micro\"}",
"Each.key is dev3. Each.value is {\"ami_id\":\"dev3ami\",\"instance_type\":\"t3.micro\"}",
]Testing against state and configuration files
If you have a state file for the current configuration, you can also use the console to interact with any values held in the local or remote state. If there is a state file, the console will lock it, meaning no other operations can be performed on it while the console is open. If you are using a remote state, that will be read before evaluating any expressions you enter into the console.
In addition to reading values from any state file, the local configuration files in the current working directory are also taken into account so expressions can be tested against both state and configuration. You will need to run terraform init first on any local configuration to generate the state file.
Here we have a file called main.tf to use for testing:
variable "region" {
type = map(any)
default = {
"uk1" = {
"region" = "uksouth",
},
"uk2" = {
"region" = "ukwest",
},
"us" = {
"region" = "eastus",
}
"us2" = {
"region" = "eastus2",
}
}
}
resource "random_password" "password" {
length = 16
special = true
override_special = "!#$%&*()-_=+[]{}<>:?"
}
variable "cidr" {
default = "172.16.0.0/20"
}With the file in the local working directory, run terraform console. We can interact with the console and experiment with different functions.
var.region
var.region.us2
var.cidr
cidrnetmask(var.cidr)
cidrhost(var.cidr, 3)
Terraform will only calculate the values of some resources after terraform apply — the console will show you this.
random_password.password
Once you have finished experimenting with different functions and expressions, hit Ctrl-C or Ctrl-D, or type exit to exit the console.
Can you define variables or modules within the Terraform console?
You cannot define variables or modules within the Terraform console. The Terraform console is primarily a REPL (read->evaluation->print->loop), so it allows you to evaluate Terraform expressions and access the state, variables, locals, and outputs.
To use a specific variable, you must first define it in your configuration.
Modules are a little bit trickier, as you need to first apply the code to access the resources or the outputs if they depend on a particular resource.
Key points
terraform console is a REPL for evaluating HCL expressions against your configuration and state. Use it to test what an expression will return before you commit it to your .tf files.
What to remember:
- The console itself doesn’t write state, configuration, or infrastructure. It reads remote state on entry and locks it for the session.
- Run
terraform initfirst, and use the-planflag to evaluate against planned values for resources that aren’t yet in state. Note that under-plan, misbehaving configurations (e.g. those using theexternaldata source) can still trigger side effects during planning. - You cannot define new variables or modules inside the console. Define them in your configuration first, then reference them by full path:
var.name,local.name,module.name.output. The same applies to OpenTofu’stofu console.
Terraform is really powerful, but managing imports, state, and configuration at scale requires more than the CLI alone. Spacelift takes Terraform management to the next level by giving you access to a powerful CI/CD workflow and features such as:
- Policy as code (based on Open Policy Agent) to enforce guardrails on every plan and apply
- Drift detection to catch resources that have changed outside of Terraform
- Multi-IaC workflows across Terraform, OpenTofu, CloudFormation, Pulumi, and Kubernetes
- Self-service infrastructure through Blueprints and Templates
- Full audit trails so you always know what changed, when, and who approved it
If you want to learn more about Spacelift, create a free account today or book a demo with one of our engineers.
Note: New versions of Terraform are placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.
Terraform management made easy
Spacelift orchestrates your Terraform workflows end to end, including state management, policy as code, drift detection, resource visualization, context sharing, programmatic configuration, and support for complex, multi-step workflows.
Frequently asked questions
How do I test Terraform expressions before applying?
Run terraform console to open an interactive REPL where you can evaluate expressions, functions, and references against your configuration and current state. For values that only exist after planning, use terraform console -plan to evaluate against the planned state.
What is the terraform console REPL?
It’s an interactive read-eval-print loop launched with terraform console that evaluates HCL expressions, built-in functions, and references to variables, locals, outputs, and state. It’s read-only and holds a state lock while open, so it can’t modify configuration or resources.
How to exit terraform console?
Type exit and press Enter, or use Ctrl-C or Ctrl-D. Closing the session releases the state lock so other Terraform operations can run.
Can I use terraform console with remote state?
Yes. When the configured backend uses remote state, Terraform reads the current workspace’s state from the backend before evaluating expressions, so references like aws_instance.example.id resolve normally.
