Day 63 - Terraform Variables

Day 63 - Terraform Variables

Variables in Terraform are quite important, as you need to hold values of names of instances, configs, etc.

We can create a 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-01

  • Create a local file using Terraform Hint:
resource "local_file" "devops" {
filename = var.filename
content = var.content
}

Create variables.tf and main.tf

Use terraform commands

$ terraform init

$ terraform plan

$ terraform validate

$ terraform fmt

$ terraform apply

Check the directory

"/home/ubuntu/terrform-tutorials/terraform-variables/"

File named demo-var.txt will be created


Data Types in Terraform

Task-02

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

  • Put proper screenshots of the outputs

Map

A Map is a collection of key-value pairs. It allows you to associate values with specific keys. Here's an example of using a Map variable

variable.tf

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

In the above code, we define a file_contents variable of type map. It contains two key-value pairs representing statements. Maps are incredibly useful for organizing related data.

main.tf

resource "local_file" "devops" {
filename = "/home/ubuntu/terraform/terraform-variable/type-map/demo.txt"
content = var.file_contents["statement1"]
}

Apply terraform commands

$ terraform init

$ terraform plan

$ terraform validate

$ terraform fmt

$ terraform apply

Check the directory

"/home/ubuntu/terraform/terraform-variable/type-map/demo.txt"

List:

A list is an ordered collection of values of the same type. To define a list variable, use the list type and specify the type of elements in the list.

variable.tf

A single variable named "file_list" has two file.txt files assigned in default

variable "file_list"{
type = list
default = ["/home/ubuntu/terraform/terraform-variable/type-map/file1.txt","/home/ubuntu/terraform/terraform-variable/type-map/file2.txt"]
}

main.tf

Here in the main.tf file, we are using the contents statement1 and statement2 from variable>File_contents and creating a file_list files in the specified directory

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

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

Apply all the terraform commands

Now check the defined directory whether the files with the given content are created or not

Object:

An object is a structural type that can contain different types of values, unlike a map, or list. It is a collection of named attributes that each have their type.

In the example, an object variable named aws_ec2_object is defined with a default value of a set of properties for an EC2 instance.

This variable can be used in your Terraform configuration to reference the individual properties of the EC2 instance defined in the object.

variable "aws_ec2_object" {
    type = object({
        name = string
        instances = number
        keys = list(string)
        ami = string
})

default = {
    name = "test-ec2-instance"
    instances = 4
    keys = ["key1.pem","key2.pem"]
    ami = "ubuntu-bfde24"
}
}

main.tf

This output can be used to display the value of the ami property in the Terraform output after applying the configuration.

output "aws-ec2-instances"{
    value = var.aws_ec2_object.ami
}

Apply terraform commands

Set:

A set is an unordered collection of unique values of the same type. To define a set variable, use the set type and specify the type of elements in the set. Here's an example:

variable "security_groups" {
  type    = set
  default = ["sg-12345678", "sg-87654321"]
}

In this example, we define a variable named "security_groups" as a set of strings with a default value.

Terraform refresh

To refresh the state of your configuration file reload the variables

terraform refresh is a Terraform command that retrieves the current state of the resources defined in your configuration and updates the Terraform state file to match that state.

By running Terraform refresh, you can update the state file to reflect the current state of your resources, so that Terraform has an accurate view of what needs to be changed. This command does not make any changes to your resources but simply updates the state file to match the current state of your resources

To refresh the state of your configuration file reloads the variables

Video Course

I can imagine, Terraform can be tricky, so best to use a Free video Course for terraform here

Happy Learning :)