# 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**](https://sutish.hashnode.dev/day-63-terraform-variables#heading-what-is-a-variable-in-terraform) file which will hold all the variables.

```plaintext
variable "filename" {
default = "/home/ubuntu/terrform-tutorials/terraform-variables/demo-var.txt"
}
```

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

These variables can be accessed by var object in [**main.tf**](https://sutish.hashnode.dev/day-63-terraform-variables#heading-what-is-a-variable-in-terraform)

# **🎯Task: 1**

* ### **Create a local file using Terraform**
    

```plaintext
# Main.tf

resource "local_file" "devops" {
filename = var.filename
content = var.content
}
```

```plaintext
# var.tf
variable "filename" {
default = "/home/ubuntu/terraform-variables/demo-var.txt"
}

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704740893766/800dd8aa-1493-4234-b500-be4418d0d2c0.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704740968668/5143e8c8-caf9-49d3-94c6-059f49920499.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704741136384/43cd307a-489b-45cd-a480-63fc79afc5f8.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704741221056/49fd48b0-0d09-48ba-b2fd-440f629be7fd.png?auto=compress,format&format=webp align="left")

* Check the file which is created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704741414773/06289e3a-d6af-4294-94a9-2792c97ac69c.png?auto=compress,format&format=webp align="left")

## **Data Types in Terraform**

## Map

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704742292138/a0dbe83f-7523-49f4-b7f7-8e6fd0ec200e.png?auto=compress,format&format=webp align="left")

* 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
    

```plaintext
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"]

}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704745835147/129077d8-8c8e-483b-8ca3-7edd9aa46c70.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704744987827/d69926d3-11a7-4566-984c-7407934913df.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704745771570/655e360d-c316-4b7b-bdb7-fdf1accce847.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704745884966/22ea6b2f-c02b-414d-9348-1f0cbfcb216f.png?auto=compress,format&format=webp align="left")

* Check the files which are created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704745988457/a91cb828-80e6-4ba2-9326-6abfc1cd0a6a.png?auto=compress,format&format=webp align="left")

### **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
    

```plaintext
variable "file_list" {
type = list
default = ["/home/ubuntu/terraform-variables/list1.txt", "/home/ubuntu/terraform-variables/list2.txt"]
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704746821447/8fe65261-93d4-47c0-9afc-b02c565fdb62.png?auto=compress,format&format=webp align="left")

Replace the file name in the [**main.tf**](https://sutish.hashnode.dev/day-63-terraform-variables#heading-what-is-a-variable-in-terraform) code to the list of file variables. This will assign the filename and location through the variable.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704747104868/f205d3bc-eca6-4c4e-81a3-f0e566b99c69.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704747139164/6a994304-14e0-447d-a1f8-2ca2a783072c.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704747184229/1297f887-0a83-4339-a503-40f3624b2cea.png?auto=compress,format&format=webp align="left")

* Check the files which are created
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704747559430/9c245323-8918-41a0-996b-a7e8e7eddf8d.png?auto=compress,format&format=webp align="left")

### **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.
    

```plaintext
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"
    }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704782422028/1d8982ce-420d-4a77-a9dd-5cff0fc19692.png?auto=compress,format&format=webp align="left")

* create an output file to view the output of a specific value
    

```plaintext
output "aws-ec2-instances"{
        value = var.ec2_instance_config
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704780106126/ca07399c-bfc1-4de2-96cd-66c565f92457.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704782473323/7d06a6ed-3670-45ae-b7b2-5e5d1995bf99.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704782528399/52e60d41-2f84-41ce-b132-a92f7e2b2c1e.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704782756826/423dc69b-ba7e-4895-9450-533a05c66153.png?auto=compress,format&format=webp align="left")

### **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.
    

```plaintext
variable "Security_groups" {
  type = set(string)
  default = ["sg-123", "sg-456" ]
  }
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788228839/b44d085a-81f5-4d7b-9ed4-0c714dd237e8.png?auto=compress,format&format=webp align="left")

* Add the Security groups variable to [main.tf](https://sutish.hashnode.dev/day-63-terraform-variables#heading-what-is-a-variable-in-terraform) file
    

```plaintext
output "Security_groups"{
    value = var.Security_groups
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788452770/55308277-a22d-4f89-838e-704b27eca1f6.png?auto=compress,format&format=webp align="left")

`terraform init`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788494354/f60611be-da6b-48c8-92b4-9dfae355c322.png?auto=compress,format&format=webp align="left")

`terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788531540/728285a5-e23a-4cfc-9766-d4115d928f30.png?auto=compress,format&format=webp align="left")

`terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704788581334/3119c9e8-48ec-4cec-89b4-28563dcc5e67.png?auto=compress,format&format=webp align="left")
