Terraform

How to Use Terraform Console Command – Examples

Terraform console command

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!

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.

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.

Terraform Console Usage & Examples

terraform split expression

Try lower("TEST")

lower terraform
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 terraform

var.region.us2

var.region.us2

var.cidr

cidrnetmask(var.cidr)

cidrnetmask(var.cidr)

cidrhost(var.cidr, 3)

cidrhost

Terraform will only calculate the values of some resources after terraform apply — The console will show you this.

random_password.password

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.

Key Points

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.

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.

Start free trial
Terraform CLI Commands Cheatsheet

Initialize/ plan/ apply your IaC, manage modules, state, and more.

Share your data and download the cheatsheet