Ever needed a quick and easy way to experiment with Terraform functions and expressions? In this short article, we will explore the terraform console
command with some examples of how to use it!
The Terraform HCL language includes a number of built-in functions that you can use in expressions.
Expressions refer to or compute values within a configuration. The simplest expressions are just literal values, like
"hello"
or5
, 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.
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.
The primary purpose of this command is to help you understand how Terraform evaluates expressions and functions based on your configuration, making it very useful for testing and debugging.
Enter the terraform console:
terraform console
I’ll 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 console
You can also use the console to interact with any values held in the local or remote state, if you have a state file for the current configuration. If there is a state file, the console will place a lock on it, meaning no other operations can be performed on it whilst the console is open. If you are using remote state, that will be read before evaluating any expressions you enter into the console.
As well as 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 I 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
. I 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, to exit the console, hit Ctrl-C or Ctrl-D, or type exit
.
That’s all there is to it! Terraform console is a great way to test out logic, check the results of functions for experimentation, and can test against your state and configuration files.
We encourage you also to explore how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this.
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.
Manage Terraform Better with Spacelift
Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.