How to use Terraform Console Command
Tags: terraform
Published: 2024-05-16 07:01:00

 ![How to use Terraform Console Command](https://www.techielass.com/content/images/2024/04/how-to-remove-a-resource-from--2-.png)

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

```

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](https://lh7-us.googleusercontent.com/HKgFeri2Th_OxVMTcek3COZ3NstXps7zo0RuB6Jo10Pv6e-Fx1gqAcPGPMXzeR4wAsDH4jxscHrFQ9pxrPBOCw4KkgpOWf8K76LI-oerrMBbPAzQWyQxUFo3HyEC1322LV-O3iW6Iz4ajRRozNVpoTM)
_Terraform console output_

Equally, you can input something like:

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

```

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](https://lh7-us.googleusercontent.com/ZR7pfLEIIYnvjUueHizHhoXDi_cKapIeyb7jacd3pq_09PLy8u14ErWTE48T6lQy1_YkCfo-UEY9VsF6U6MxdaqawA4AXjojCiXiiqzwJQjRZXWvu4Kb-abPfTsnpDtX_5a3WOtiG5lqi16TxKDo4oE)
_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
}

```

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](https://lh7-us.googleusercontent.com/RdD-lZkd8ar6mgkT75c92T3M0JhAmgz0UW3Ni6UfO1pHOkOnKZbNRC-DXJIzNdUvukg-5HIB397plPi47Ryie5M4k1P05zuygGmL-MHz5-De3FJN24jDMdyI1Z7YsPvtJ9jjg4DZe7mTPnfINuhOnX0)
_terraform output_

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

3. 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. 

4. 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](https://lh7-us.googleusercontent.com/yvoAL8hSQJ7BWGnq8qi0jWBE-9M7ES1E0QrtB40qkY1scH8bN7J4kpkR7lWA_niJNxs3CEkCdmU7hSUzm2gen-dP5F0PiIzbWZ3YAejCZ3e7B6s1eqxK5QiAjFgZ9vIVzH5IU0zBKmsf7b0LqZcPcAo)
_terraform output_

5. 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.

{% embed https://youtu.be/GUax2r03hmA %}