DEV Community

Sarah Lean 🏴󠁧󠁒
Sarah Lean 🏴󠁧󠁒

Posted on β€’ Originally published at techielass.com

3

How to use Terraform Console Command

How to use Terraform Console Command

The terraform console command allows you to experiment with Terraform functions and expressions. In this blog post, we will explore how to use the command.

What is the Terraform console command?

The terraform console command is part of the Terraform CLI. It will open an interactive console where you can experiment and test expressions before using them in production.

Terraform Console Examples

To enter the Terraform console type in the following command



terraform console



Enter fullscreen mode Exit fullscreen mode

From here you can try and test several different scenarios.

The first example to try is some arithmetic, you can type in 2 + 5 and the terraform console will give you an output.

How to use Terraform Console Command
Terraform console output

Equally, you can input something like:



format("Hello, %s!", "Techielass")



Enter fullscreen mode Exit fullscreen mode

In this example, the format function is used to create a string with a placeholder %s that is replaced by the value "Techielass".

The resulting string "Hello, Techielass!" is returned by the function.

How to use Terraform Console Command
Terraform console output

You can also use the terraform console command to test and query Terraform files.

Let’s take an example of Terraform code for deploying an Azure resource group using the Azure provider:



# Define Azure provider
provider "azurerm" {
  features {}
}

# Define variables
variable "resource_group_name" {
  type    = string
  default = "techielass-resource-group"
}

variable "location" {
  type    = string
  default = "UK South"
}

# Create Azure resource group
resource "azurerm_resource_group" "example" {
  name     = var.resource_group_name
  location = var.location
}



Enter fullscreen mode Exit fullscreen mode

Code explanation:

  • Provider Block : The provider block specifies that we are using the azurerm provider, which allows Terraform to interact with Azure resources.
  • Variables : Variables are used to parameterize the Terraform configuration. In this example, we have defined two variables:

    • resource_group_name: Specifies the name of the Azure resource group to be created. This variable has a default value of "techielass-resource-group", which can be overridden when running Terraform.
    • location: Specifies the Azure region where the resource group will be deployed. It has a default value of "UK South".
  • Resource Block : The resource block defines the Azure resource to be managed by Terraform. In this case, we are creating an Azure resource group using the azurerm_resource_group resource type. The name attribute is set to the value of the resource_group_name variable, and the location attribute is set to the value of the location variable.

To test and query Terraform files using the terraform console :

  1. First, ensure that you have initialized the Terraform configuration in the directory containing your .tf files by running terraform init.

How to use Terraform Console Command
terraform output

  1. Once the initialization is complete, you can open the Terraform console by running terraform console.

  2. In the Terraform console, you can interactively evaluate expressions and query Terraform configuration elements. For example, you can query the value of variables or perform calculations.

  3. To use variables, you can simply reference them by name. For instance, you can type var.resource_group_name to see the current value of the resource_group_name variable.

How to use Terraform Console Command
terraform output

  1. To exit the Terraform console, you can type exit or press Ctrl+D.

Conclusion

In summary, the Terraform console command offers an interactive environment to experiment with Terraform functions and expressions before deploying them in production. By incorporating this tool into your Terraform workflow, you can streamline development processes and ensure the reliability of your infrastructure deployments.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (1)

Collapse
 
duncan_true profile image
Dun β€’

Could you explain more about how the format function works with different data types in the terraform console? Great post, by the way!

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay