Day 63 Task:  Terraform Variables

Day 63 Task: Terraform Variables

In Terraform, variables are used to parameterize your configuration so that you can reuse and customize your infrastructure definitions. Variables allow you to define values that can be passed into your Terraform configuration when you apply it.

We can create a variable. 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 the var object in main. tf

🔶 Data Types in Terraform

Terraform supports several data types that you can use when defining variables, resources, and other elements in your infrastructure as code (IaC) configurations. Here are some of the key data types in Terraform:

  1. String: Represents a sequence of characters. Strings are commonly used for defining text values like names, descriptions, and labels.

    Example:

     variable "app_name" {
       type    = string
       default = "my-app"
     }
    
  2. Number: Represents numeric values, including both integers and floating-point numbers. Numbers are used for specifying quantities, sizes, and numeric parameters.

    Example:

     variable "instance_count" {
       type    = number
       default = 2
     }
    
  3. Bool: Represents boolean values, which can be either true or false. Booleans are used for specifying conditional logic.

    Example:

     variable "enable_logging" {
       type    = bool
       default = true
     }
    
  4. List: Represents an ordered collection of values. Lists are useful when you need to specify multiple items of the same data type, such as a list of strings or numbers.

    Example:

     variable "availability_zones" {
       type    = list(string)
       default = ["us-east-1a", "us-east-1b", "us-east-1c"]
     }
    
  5. Map: Represents a collection of key-value pairs. Maps are used when you need to associate keys with values, such as defining resource tags.

    Example:

     variable "tags" {
       type = map(string)
       default = {
         Name        = "my-instance"
         Environment = "production"
       }
     }
    
  6. Object (Complex Types): Terraform allows you to define complex data structures as objects, which can contain multiple attributes with different data types. Objects are often used for defining custom data structures.

    Example:

     variable "user" {
       type = object({
         username = string
         email    = string
         age      = number
       })
       default = {
         username = "johndoe"
         email    = "john@example.com"
         age      = 30
       }
     }
    

These are some of the core data types in Terraform. We can use these data types when defining variables, input values, and resource attributes to parameterize and customize your infrastructure definitions. Additionally, Terraform provides advanced data types and functions for working with more complex data structures and operations.

🔶 Task 01: Create a local file using Terraform

Create a terraform configuration file main.tf

#main.tf
resource "local_file" "devops" {
filename = var.filename
content = var.content
}

Create a variable.tf to define the input variables filename and content.

variable "filename" {
default = "/home/ubuntu/Day63/demo-var.txt"
}

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

Now Save and run the command terraform init to initialize terraform and install all the plugins and providers.

Run terraform plan to outline the required changes to achieve the planned infrastructure.

Now run terraform apply to create the file.

To check and verify whether the file has been created or not use command ll.


🔶 Task 02: Use terraform to demonstrate usage of List, Set and Object datatypes

  1. Create a new Terraform configuration file and define variables that utilize different data types, such as lists, sets, and objects. I will use the below variable.tf:

      variable "filename_list" {
        type    = list(string)
        default = ["/home/ubuntu/Day63/tasktwo/list_task_1.txt", "/home/ubuntu/Day63/tasktwo/list_task_2.txt"]
      }
    
      variable "content_list" {
        type    = list(string)
        default = ["Apple", "Mango", "Banana"]
      }
    
      variable "servers_set" {
          type = set(string)
          default = ["Ubuntu", "Windows"]
      }
    
      variable "object_server" {
        type = object({
          name     = string
          cpu      = number
          memory   = number
          disk     = bool
          os       = string
        })
        default = {
          name     = "web-server"
          cpu      = 4
          memory   = 8
          disk     = true
          os       = "Ubuntu 22.04"
        }
      }
    

    Add the necessary resources and configurations to your Terraform file to showcase the usage of these data types. This is my main.tf file:

      resource "local_file" "list_file_1" {
          filename = var.filename_list[0]
          content = var.content_list[0]
      }
    
      resource "local_file" "list_file_2" {
          filename = var.filename_list[1]
          content = var.content_list[2]
      }
    
      output "server_set_names" {
          value = var.servers_set
      }
    
      output "object_server_name" {
          value = var.object_server.name
      }
    

    Save the changes to your Terraform configuration file and run terraform init to initiate the file.

    Run terraform plan to outline the required changes to achieve the planned infrastructure.

    Now run terraform apply to create the file.

    To check and verify whether the file has been created or not use command ll.

Use terraform refresh: To refresh the state of your configuration file, reload the variables.


Happy Learning :)

Stay in the loop with my latest insights and articles on cloud ☁️ and DevOps ♾️ by following me on Hashnode, LinkedIn (https://www.linkedin.com/in/chandreshpatle28/), and GitHub (https://github.com/Chandreshpatle28).

Thank you for reading! Your support means the world to me. Let's keep learning, growing, and making a positive impact in the tech world together.

#Git #Linux Devops #Devopscommunity #90daysofdevopschallenge #python #docker #Jenkins #Kubernetes #Terraform

Did you find this article valuable?

Support Chandresh Patle's Blog by becoming a sponsor. Any amount is appreciated!