Skip to main content

Command Palette

Search for a command to run...

Day 63 - Terraform Variables (Jan 28, 2024)

Updated
4 min read
Day 63 - Terraform Variables  (Jan 28, 2024)

🙏 Introduction:

In this blog, we will learn Terraform variables, exploring various data types such as Map, List, Set, and Object.

🔶What is a Variable in Terraform?

In Terraform, variables are used to define inputs to your Terraform configuration. They allow you to parameterize your code and provide dynamic values to your infrastructure. Variables can be used to pass values between different parts of your Terraform code or to allow users to customize the behaviour of your infrastructure.

We can create variables.tf file which will hold all the variables.

variable "filename" {
default = "/home/ubuntu/terrform-tutorials/terraform-variables/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable which was updated"
}

These variables can be accessed by var object in main.tf

🎯Task: 1

  • Create a local file using Terraform

# Main.tf

resource "local_file" "devops" {
filename = var.filename
content = var.content
}
# var.tf
variable "filename" {
default = "/home/ubuntu/terraform-variables/demo-var.txt"
}

variable "content" {
default = "This is coming from a variable which was updated"
}

terraform init

terraform plan

terraform apply

  • Check the file which is created

Data Types in Terraform

Map

variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}

🎯Task: 2

  • Use terraform to demonstrate usage of List, Set and Object datatypes

Map

In Terraform, a map is a data type that allows you to represent a collection of key-value pairs. It is similar to dictionaries or hash tables in other programming languages. Maps are useful for organizing and managing data in a structured way

variable "file_contents" {
 type = map
 default = {
 "statement1" = "this is cool"
 "statement2" = "this is cooler"
 }
 }

  • This variable can be used in your Terraform configuration to reference the values associated with each key defined in the map. For example, you might use it to configure a resource that requires content
resource "local_file" "devops" {
    filename = "/home/ubuntu/terraform-variables/map1.txt"
    content = var.file_contents["statement1"]
}

resource "local_file" "devops_var" {
    filename = "/home/ubuntu/terraform-variables/map2.txt"
    content = var.file_contents["statement2"]

}

terraform init

terraform plan

terraform apply

  • Check the files which are created

List

In Terraform, a list is an ordered collection of values where all elements must have the same type. It is defined using square brackets

  • Create a variable of type list to pass the list of files
variable "file_list" {
type = list
default = ["/home/ubuntu/terraform-variables/list1.txt", "/home/ubuntu/terraform-variables/list2.txt"]
}

Replace the file name in the main.tf code to the list of file variables. This will assign the filename and location through the variable.

resource "local_file" "devops" {
    filename = "var.file_list[0]"
    content = var.file_contents["statement1"]
}

resource "local_file" "devops_var" {
    filename = "var.file_list[1]"
    content = var.file_contents["statement2"]

}

terraform init

terraform plan

terraform apply

  • Check the files which are created

Object

In Terraform, an object is a complex data structure that allows you to store and manage a collection of key-value pairs, where the value can be of different data types, including other objects, lists, or maps. Objects are often used to define resource configurations that require complex or nested data structures.

  • Create a variable to store the object of ec2 instance that will contain some of the instance configurations.
variable "ec2_instance_config" {
  type = object({
        name = string
        instances = number
        key_name = string
        ami = string
    }
)

default = {
    name = "test"
    instances = 2
    key_name = "terra_key.pem"
    ami = "ami-008fe2fc65df48dac"
    }
}

  • create an output file to view the output of a specific value
output "aws-ec2-instances"{
        value = var.ec2_instance_config
}

terraform init

terraform plan

terraform apply

Set

In Terraform, sets are commonly used to represent a group of distinct values. You can use sets in various situations, such as when defining variable types, managing resource dependencies, or creating dynamic configurations.

  • Create a variable for the type set to pass the string of security groups.
variable "Security_groups" {
  type = set(string)
  default = ["sg-123", "sg-456" ]
  }

  • Add the Security groups variable to main.tf file
output "Security_groups"{
    value = var.Security_groups
}

terraform init

terraform plan

terraform apply

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.