Skip to main content

Command Palette

Search for a command to run...

Terraform Variables and Data Type

Published
3 min readView as Markdown
Terraform Variables and Data Type
M

Tech enthusiast | Code craftsman | DevOps explorer | Turning bugs into features one line at a time 🚀

What are variables in terraform?

In Terraform, variables are parameters that you can use to input values into your configurations. They allow you to define flexible and dynamic Terraform configurations that can be customized based on different use cases or environments.

Variables are declared using the variable block in your Terraform configuration files.

Task-01: Create a local file using Terraform

Step 1: Create variable.tf

Create a file named variable.tf with the following content:

Copy

Copy

variable "file_content" {
  type    = string
  default = "This is the content of the local file.\n"
}

variable "file_path" {
  type    = string
  default = "/path/to/myfile.txt"
}

This step defines two variables:

  • file_content: Contains the content you want to write to the local file.

  • file_path: Specifies the path where the local file will be created.

Step 2: Create main.tf

Create a file named main.tf with the following content:

Copy

Copy

provider "local" {}

resource "local_file" "example" {
  content  = var.file_content
  filename = var.file_path
}

This step defines a local_file resource that will create a local file with the specified content and path.

Step 3: Initialize and Apply

Run the following commands in the terminal:

Copy

Copy

terraform init
terraform apply

Terraform will prompt you to confirm the creation of the local file. Type yes to proceed.

Step 4: Verify

Check the specified path (/path/to/myfile.txt in this example) to confirm that the local file has been created with the specified content.

Task -02: Data Types in Terraform

In Terraform, data types are used to define the kind of values that can be assigned to variables and used in resource configurations.

Terraform has several built-in data types. Here are some of the key data types in Terraform:

  1. String: Represents a sequence of characters. Strings are used for various purposes, such as defining resource names, tags, and other textual values.

    Copy

    Copy

      variable "example_string" {
        type    = string
        default = "Hello, Terraform!"
      }
    
  2. Number: Represents numeric values, either integers or floating-point numbers.

    Copy

    Copy

      variable "example_number" {
        type    = number
        default = 42
      }
    
  3. Bool: Represents boolean values, either true or false.

    Copy

    Copy

      variable "example_bool" {
        type    = bool
        default = true
      }
    
  4. List: Represents an ordered collection of values. Lists are useful when you need to work with multiple items of the same type.

    Copy

    Copy

      variable "example_list" {
        type    = list(string)
        default = ["item1", "item2", "item3"]
      }
    
  5. Map: Represents a collection of key-value pairs. Maps are useful for associating values with specific keys.

    Copy

    Copy

      variable "example_map" {
        type    = map(string)
        default = {
          key1 = "value1"
          key2 = "value2"
        }
      }
    
  6. Object: Represents a complex data structure with attributes and nested blocks. Objects are useful for modeling more structured data.

    Copy

    Copy

      variable "example_object" {
        type = object({
          name    = string
          age     = number
          is_male = bool
        })
    
        default = {
          name    = "John Doe"
          age     = 30
          is_male = true
        }
      }
    
  7. Set: A set is an unordered collection of unique values. It's used for managing distinct elements, and the order doesn't matter. Each element must be unique.

    Copy

    Copy

      variable "example_set" {
        type    = set(string)
        default = ["element1", "element2", "element3"]
      }
    
  8. Tuple: A tuple is an ordered collection of values where each element can have a different data type. Tuples group related data in a specific order, and you access elements by their index.

    Copy

    Copy

      variable "example_tuple" {
        type    = tuple([string, number, bool])
        default = ["value1", 42, false]
      }
    

More from this blog

Untitled Publication

78 posts