🚀Day 63 - Terraform Variables🔥

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

·

3 min read

🚀Day 63 - Terraform Variables🔥

Dear learners, As you know, In the Previous lecture, We discussed the Docker with Terraform. Now in today's lecture discuss the Terraform Variables with its working.

variables in Terraform are quite important, as you need to hold values of names of instance, 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:
# Main.tf

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

variable "filename" {
default = "/home/ubuntu/terraform_files/devops.txt"
}


variable "content" {
default = "This is code"
}

Terraform and variable file:

terraform init

terraform plan

terraform apply

Result:

Data Types in Terraform

Map

# Main.tf

resource "local_file" "file_contents" {
filename = var.file_contents["statement1"]
content = var.file_contents["statement2"]
}
# var.tf

variable "file_contents" {
type = map
default = {
"statement1" = "Demo.txt"
"statement2" = "this is cooler"
}
}

variable file:

Terrform_file.tf file

terraform apply

Result:

Task-02

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

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. Here’s an example:

# terrform_file.tf

resource "local_file" "devops" {
    filename = var.file_list[0]
    content = var.content_map["content1"]
}

resource "local_file" "devops1" {
    filename = var.file_list[1]
    content = var.content_map["content2"]
}
# var.tf

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

variable "content_map" {
type = map
default = {
"content1" = " I am sanjana "
"content2" =  "Hello guys"
}
}

$ terraform init

$ cat terrform_file.tf

$ cat var.tf

$ terraform plan

$ terraform apply

Result:

* Object : Unlike a map and list, an object is a structural type that can hold several types of values. It is a set of named attributes, each with its own type.

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

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

# var.tf

variable "aws_ec2_object" {
    type = object({
        name = string
        instances = number
        keys = string
        ami = string
})
default = {
    name = "test-ec2-instance"
    instances = 1
    keys = "key1.pem"
    ami = "ubuntu-bfde24"
}
}
# main.tfoutput "aws-ec2-instances"{
    value = var.aws_ec2_object.ami
}

*terraform refresh

Reloads the variables to refresh the state of your configuration file.

The Terraform refresh command retrieves the current state of the resources defined in your configuration and refreshes the Terraform state file to match that state.

You can update the state file to reflect the current state of your resources by running Terraform refresh, so Terraform has an accurate perspective of what needs to be updated. This command makes no modifications to your resources; instead, it just updates the state file to reflect the current state of your resources.

Use terraform refresh

To refresh the state by your configuration file, reloads the variables

_Thank you

Â