Terraform Variables & Datatypes

Abhiraj Thakur
7 min readNov 11, 2023

--

Terraform variables allow you to parameterize your configurations, making them flexible and reusable.

In this blog, I will cover how to use Terraform variables. I will show you some hands-on practical which includes:

  • Creating local file
  • Demonstrating Terraform variables.

Creating Terraform variables:

Create a new file, variables.tf, in which we define variables:

variable "filename" {
type = string
default = "/home/abhiraj/Tasks/Day63/day63.txt"
}

variable "content"{
type = string
default = "Hello flocks, it is day 63 of 90 days of DevOps"
}

Explanation:

filename Variable:

  • Type: Specifies that the filename variable expects a value of type string.
  • Default Value: Provides a default value for the variable. If no value is provided when using this Terraform configuration, it will default to “/home/abhiraj/Tasks/Day63/day63.tf”.
  • Purpose: This variable represents the file path where you want to create a local file using Terraform.

content Variable:

  • Type: Specifies that the content variable expects a value of type string.
  • Default Value: Provides a default value for the variable. If no value is provided when using this Terraform configuration, it will default to “Hello flocks, it is day 63 of 90 days of DevOps”.
  • Purpose: This variable represents the content that you want to write into the local file created by Terraform.

Also, create a new file resource.tf:

resource "local_file" "day63" {
filename = var.filename
content = var.content
}

Explanation:

Resource Type and Name:

  • local_file: Specifies that you're using the local_file resource type, which allows you to create a local file on the system where Terraform is executed.
  • "day63": This is the local identifier or name given to this specific instance of the local_file resource. You can use this name to reference this resource elsewhere in your Terraform configuration.

Block Content:

  • filename Attribute: Specifies the filename for the local file. It is set to the value of the filename variable using var.filename. This allows you to dynamically set the filename based on the value provided to the filename variable.
  • content Attribute: Specifies the content that will be written to the local file. It is set to the value of the content variable using var.content. This allows you to dynamically set the content based on the value provided to the content variable.

To create a new file, with the defined content follow the below steps:

Step 1: Initialize Terraform directory:

terraform init

Step 2: Plan the Terraform resources

terraform plan

Step 3: Creating resources:

terraform apply

Enter yes to confirm the creation of resources.

Step 4: Validating the resources:

Let’s see that the file has been created with the defined content.

ls

Here, you can see that day63.txt has been created, let’s see the content in that file.

cat day63.txt

You can see that in the created file the content has been added which we want.

Step 5: Deleting the created Terraform resources:

Use the below command to delete the Terraform resources:

terraform destroy

Type yes to confirm.

Step 6: Validating:

ls to see that day63.txt has been deleted.

Here you can see that there is no such file day63.txt, which means that the resource has been deleted.

Terraform Datatypes:

Terraform supports several data types, providing flexibility in defining variables and working with different kinds of data. Data types can be used to define variables, input values, and outputs in your Terraform configurations.

In this blog, I will be covering the following data types:

  • List
  • Set
  • Objects

List:

# Define a variable of type list
variable "list_example" {
type = list(string)
default = ["item1", "item2", "item3"]
}

# Print each item in the list
output "list_output" {
value = var.list_example
}

Explanation:

Define a Variable of Type List:

  • Type: Specifies that the variable list_example is of type list, containing string elements.
  • Default: Provides a default value for the list, which includes three string items: “item1”, “item2”, and “item3”.

Output the List:

  • Output Block: Defines an output block named list_output.
  • Value: Sets the value of the output to the content of the list_example variable using var.list_example.

Usage:

  • Output Block: Defines an output block named list_output.
  • Value: Sets the value of the output to the content of the list_example variable using var.list_example.

Set:

# Define a variable of type set
variable "set_example" {
type = set(string)
default = ["item1", "item2", "item3"]
}

# Print the set
output "set_output" {
value = var.set_example
}

Explanation:

Variable Definition:

  • Variable Name: set_example is the name of the variable.
  • Variable Type: type = set(string) specifies that the variable is a set containing elements of type string.
  • Default Value: default = ["item1", "item2", "item3"] provides a default set with three string elements: "item1", "item2", and "item3".

Output Definition:

  • Output Block: Defines an output block named set_output.
  • Output Value: value = var.set_example sets the value of the output to the content of the set_example variable, which is the set of string elements.

Usage:

When you run terraform apply:

  • Terraform will use the default values specified for the set_example variable.
  • It will then print the output, showing the elements of the set.

Object:

# Define a variable of type object
variable "object_example" {
type = object({
key1 = string
key2 = number
key3 = bool
})

default = {
key1 = "value1"
key2 = 42
key3 = true
}
}
output "list_output" {
value = var.object_example
}

Explanation:

Variable Definition:

  • Variable Name: object_example is the name of the variable.
  • Variable Type: type = object({ key1 = string, key2 = number, key3 = bool }) specifies that the variable is of type object. The object has three keys: key1, key2, and key3, each with a specific data type.

Default Value:

  • Provides a default value for the object_example variable. The default value is an object with values assigned to each key. The values are "value1" for key1, 42 for key2, and true for key3.

Output Definition:

  • Output Block: Defines an output block named list_output.
  • Output Value: value = var.object_example sets the value of the output to the content of the object_example variable.

Usage:

When you run terraform apply, Terraform will use the default values specified for the object_example variable and print the output:

Terraform Refresh:

The terraform refresh command in Terraform is used to update the state file (terraform.tfstate) with the real-world infrastructure. It queries the current state of the resources defined in your configuration and updates the state file to reflect the existing state of those resources.

You can use this command for:

  1. Current State Synchronization:
  • Terraform maintains a state file that represents the desired state of your infrastructure as defined in your Terraform configuration.
  • The actual state of resources in your cloud provider or infrastructure might change due to external factors (manual changes, changes made by other tools, etc.).

2. Detecting Changes:

  • terraform refresh is used to detect any changes in the real-world infrastructure that are not reflected in the state file.
  • It queries the actual state of resources by making API calls to the infrastructure provider.

3. Updating State File:

  • Once differences are detected, terraform refresh updates the state file to reflect the current state of resources.
  • It does not make any changes to the infrastructure; it only updates the local state.

3. Use Cases:

  • After running terraform apply, manual changes might be made to the infrastructure outside of Terraform.
  • terraform refresh is useful to synchronize the state file with the actual state of the infrastructure before making any further changes.

--

--

No responses yet