Terraform Variables

Terraform Variables

·

8 min read

In Terraform, we use variables to bring in values from outside sources into our Terraform setup. They let us set up the infrastructure without putting specific values directly into the configuration. Variables are crucial in Terraform, especially when dealing with names of instances and configurations.

To make things organized, we make a file called variables.tf to keep all our variables together. Here's an example:

# variables.tf

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

variable "content" {
  default = "This file is a demo of Terraform variables."
}

This way, we keep everything tidy and have a central place to manage our variables for a smoother and more scalable Terraform setup.

Task-01

Create a local file using Terraform.

Create a variable.tf file

In this file, we set up two variables. One called filename stores the path where the local file will be created, and the other, content, holds the text that will be written to the local file.

Create a main.tf file

In this file, we use Terraform to create a resource called local_file named "devops." This resource is like a set of instructions for Terraform to follow. It tells Terraform to create a file on your computer with the name and content specified by the filename and content variables.

Here's a breakdown:

  • resource "local_file" "devops": We're telling Terraform to create a local file resource named "devops."

  • filename = var.filename: This sets the filename of the local file to the value of the filename variable.

  • content = var.content: This sets the content of the local file to the value of the content variable.

After making these files, follow these steps:

  1. Get Ready: Start by setting things up:

     terraform init
    

  2. Preview Changes: Check out what Terraform plans to do:

     terraform plan
    

  3. Make It Happen: If the plan looks good, go ahead:

     terraform apply
    

    Confirm by typing "yes."

  4. See the New File: After applying, make sure the file is there:

     ls /home/ubuntu/terraform/terraform-variable/
    

    Look for a file called var-demo.txt in that spot.

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 like a labelled collection of values, where each value is identified by a unique string label. It is like dictionary in programming languages i.e having key-value pair.

variables.tf:

# variables.tf

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

This part introduces a Terraform variable called content_map.

  • variable "content_map": Declares a variable named content_map. This variable is a map, which is a way of organizing data with labels.

  • type = map: Specifies that the variable is of type map. In Terraform, a map is a structure that holds key-value pairs.

  • default = { "content1" = "this is cool content1" "content2" = "this is cooler content2" }: Sets a default value for the content_map variable. The default value is a map with two key-value pairs. The keys are "content1" and "content2," and their corresponding values are the strings "this is cool content1" and "this is cooler content2."

Now, let's explore how this variable is utilized in the Terraform configuration:

main.tf:

# main.tf

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

resource "local_file" "devops_var" {
  filename = var.filename
  content  = var.content_map["content2"]
}

In this part, the content_map variable is applied within two local_file resources:

  • resource "local_file" "devops": Declares a resource of type local_file with the name "devops."

    • filename = "/home/ubuntu/terraform/terraform-variable/type-map/demo.txt": Specifies the filename for the local file, defining a specific path and filename.

    • content = var.content_map["content1"]: Sets the content of the local file to the value associated with the key "content1" in the content_map variable. In this case, it will be "this is cool content1" based on the default value set earlier.

  • resource "local_file" "devops_var": Declares a second resource of type local_file with the name "devops_var."

    • filename = var.filename: Specifies the filename for this new local file, taking its value from the filename variable.

    • content = var.content_map["content2"]: Sets the content of this local file to the value associated with the key "content2" in the content_map variable. In this case, it will be "this is cooler content2" based on the default value set earlier.

When you execute Terraform and apply this configuration, it will generate two local files. The first, named demo.txt in the specified path, will have the content "this is cool content1." The second file will use a filename specified by the filename variable and have the content "this is cooler content2."

List:

A list is like a well-ordered group of values, all of the same type. To declare a list variable, specify the list type and indicate the type of elements within the list. Here's an illustration:

variables.tf:

# variables.tf

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

In this snippet, we define a variable named "file_list" as a list of strings with a default value containing file paths.

  • variable "file_list": Declares a variable named "file_list" as a list. This variable represents an ordered collection of values.

  • type = list: Specifies that the variable is of type list. In Terraform, a list is a sequential collection of elements.

  • default = ["/home/ubuntu/terraform/terraform-variable/type-map/file1.txt", "/home/ubuntu/terraform/terraform-variable/type-map/file2.txt"]: Sets a default value for the "file_list" variable. The default value is a list containing two strings representing file paths.

Now, let's explore how this variable is utilized in the Terraform configuration:

main.tf:

# main.tf

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

In this example, the "file_list" variable is applied to set the filename parameter of a local_file resource.

  • resource "local_file" "devops": Declares a resource of type local_file with the name "devops."

    • filename = var.file_list[0]: Sets the filename of the local file to the first element of the "file_list" variable using square bracket notation and var.file_list[0] syntax.

    • content = var.content_map["content1"]: Sets the content of the local file to the value associated with the key "content1" in the content_map variable.

After defining these variables and resources, you can run the following Terraform commands:

terraform init
terraform plan
terraform apply

Upon applying, check for the creation of two files with different content based on the specified paths and the content associated with the variables in your Terraform configuration.

Object:

Object: An object is like a container that can hold different types of values, making it distinct from maps and lists. It's a collection of named attributes, each with its own type.

variables.tf:

# variables.tf

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

In this section, we define a Terraform variable named aws_ec2_object as an object with a default value. The object includes properties for configuring an EC2 instance.

  • variable "aws_ec2_object": Declares a variable named aws_ec2_object as an object. This variable represents a structured collection of attributes.

  • type = object({...}): Specifies the type of the variable, defining its structure with named attributes and their respective types.

  • default = {...}: Sets a default value for the aws_ec2_object variable. The default value is an object with properties like "name," "instances," "keys," and "ami," each configured with specific values.

main.tf:

# main.tf

output "aws_ec2_instances" {
  value = var.aws_ec2_object.ami
}

In the main.tf file, we use an output block to display the value of the "ami" property in the Terraform output after applying the configuration.

  • output "aws_ec2_instances": Declares an output named "aws_ec2_instances."

    • value = var.aws_ec2_object.ami: Specifies that the output should display the value of the "ami" property from the aws_ec2_object variable.

After defining these variables and outputs, you can run the following Terraform commands:

terraform init
terraform plan
terraform apply

Upon applying, you will see the output displaying the value of the "ami" property in the Terraform output.

Set:

A set is like a bag of unique values, where the order doesn't matter. To declare a set variable, use the set type and specify the type of elements in the set.

variables.tf:

# variables.tf

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

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

  • variable "security_groups": Declares a variable named "security_groups" as a set. This variable represents an unordered collection of unique values of the same type.

  • type = set(string): Specifies that the variable is of type set, and the elements within the set are of type string.

  • default = ["sg-12345678", "sg-87654321"]: Sets a default value for the "security_groups" variable. The default value is a set containing two strings representing security group IDs.

Now, you can use this variable in your Terraform configuration to reference the set of security group IDs.

**

Refreshing Terraform State:**

Using terraform refresh is a command in Terraform that helps you synchronize the state of your resources with the configuration file. When you run terraform refresh, it retrieves the current state of the resources defined in your configuration and updates the Terraform state file to accurately reflect that state.

Command Usage:

terraform refresh

Explanation:

Running terraform refresh serves to update the state file without making any actual changes to your resources. It ensures that the Terraform state file is in sync with the real-world state of your infrastructure.


Thankyou for reading until here. See you in the next one.