# Terraform Variables and Data Type

# **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`](http://variable.tf/)

Create a file named [`variable.tf`](http://variable.tf/) with the following content:

**Copy**

**Copy**

```plaintext
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`](http://main.tf/)

Create a file named [`main.tf`](http://main.tf/) with the following content:

**Copy**

**Copy**

```plaintext
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**

```plaintext
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. **<mark>String</mark>:** Represents a sequence of characters. Strings are used for various purposes, such as defining resource names, tags, and other textual values.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_string" {
       type    = string
       default = "Hello, Terraform!"
     }
    ```
    
2. **<mark>Number</mark>:** Represents numeric values, either integers or floating-point numbers.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_number" {
       type    = number
       default = 42
     }
    ```
    
3. **<mark>Bool</mark>:** Represents boolean values, either `true` or `false`.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_bool" {
       type    = bool
       default = true
     }
    ```
    
4. **<mark>List</mark>:** Represents an ordered collection of values. Lists are useful when you need to work with multiple items of the same type.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_list" {
       type    = list(string)
       default = ["item1", "item2", "item3"]
     }
    ```
    
5. **<mark>Map</mark>:** Represents a collection of key-value pairs. Maps are useful for associating values with specific keys.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_map" {
       type    = map(string)
       default = {
         key1 = "value1"
         key2 = "value2"
       }
     }
    ```
    
6. **<mark>Object</mark>:** Represents a complex data structure with attributes and nested blocks. Objects are useful for modeling more structured data.
    
    **Copy**
    
    **Copy**
    
    ```plaintext
     variable "example_object" {
       type = object({
         name    = string
         age     = number
         is_male = bool
       })
    
       default = {
         name    = "John Doe"
         age     = 30
         is_male = true
       }
     }
    ```
    
7. <mark>Set</mark>: 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**
    
    ```plaintext
     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**
    
    ```plaintext
     variable "example_tuple" {
       type    = tuple([string, number, bool])
       default = ["value1", 42, false]
     }
    ```
