Day 63 - Terraform Variables

Day 63 - Terraform Variables

n Terraform, variables pass values from external sources into Terraform configuration. They provide a way to configure the infrastructure without hardcoding values directly into the configuration. Variables in Terraform are important, as you need to hold values of names of instances, configs, etc.

We can create a variable.tf file which will hold all the variables.

variable "filename" {
default = "/home/ubuntu/terraform/terraform-variable/var-demo.txt"
}
variable "content" {
default = "This is demo terraform variable file"
}

Task-01

Create a local file using Terraform.

Create a variable.tf file

No alt text provided for this image

The filename variable is set to /home/ubuntu/terraform/terraform-variable/var-demo.txt by default, which is the path and filename where the local file will be created.

The content variable has the default value of "This is demo terraform variable file", which is the text that will be put into the local file.

Create a main.tf file

No alt text provided for this image

The "devops" local_file resource, which is created by Terraform code block, write a file with the name and content given by the filename and content variables to disc.

initializes a new or existing working directory for Terraform

No alt text provided for this image

Produces an execution plan outlining the steps Terraform will take to achieve the desired state defined in the configuration file.

No alt text provided for this image

When you run Terraform apply, Terraform will generate a file in the local directory named var-demo.txt with the content supplied in the content variable.

No alt text provided for this image

Check file is created in a specified folder using the ls command.

No alt text provided for this image

Data Types in Terraform

Map

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

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 values where each value is identified by a string label

variable.tf

variable "content_map" {
type = map
default = {
"content1" = "this is cool content1"
"content2" = "this is cooler content2"
}
}

A map variable named content_map is defined in the example, with a default value of two key-value pairs. Strings serve as both the keys and the values.

No alt text provided for this image

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,

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

No alt text provided for this image

In this example, we set the content parameter of the local_file resource using the content_map variable. We use dot notation and the syntax var.content_map["content1"] to refer to the value associated with the key "content1."

No alt text provided for this image

terraform plan

No alt text provided for this image

terraform apply

No alt text provided for this image

No alt text provided for this image

Check the demo.txt file content

No alt text provided for this image

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:

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

No alt text provided for this image

In this example, the term "file_list" refers to a list of strings with the default value being a list of strings containing file paths.

You can use this variable in your Terraform setup to refer to the list of defined file paths.

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

No alt text provided for this image

In this example, we set the filename parameter of a local_file resource using the file_list variable. We use square bracket notation and the syntax var.file_list[0] to refer to the first element of the list.

Terraform plan

No alt text provided for this image

Terraform apply

No alt text provided for this image

No alt text provided for this image

Check 2 files created with different content

No alt text provided for this image

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.

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

No alt text provided for this image

main.tf

This output can be used to show the value of the ami property in the Terraform output after the configuration has been applied.

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

No alt text provided for this image

No alt text provided for this image

terraform apply

you can see the output of aws-ec2-instance value.

No alt text provided for this image

Set:

A set is an unsorted collection of distinct values of the same type. Use the set type to define a set variable and specify the type of components in the set. Here's an illustration:

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

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.

No alt text provided for this image