Day 63: Terraform Variables

Day 63: Terraform Variables

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 file which will hold all the variables.

Here we use Terraform Variables to create a local file using Terraform Block types like provider, resource, variable, output, etc.

  • Example of Provider:
provider "aws" {
  region = "us-east-1"
}
  • Example of Resource:
resource "aws_instance" "example" {
    ami = "ami-0c55b159cbfafe1f0"
    instance_type = "t2.micro"
}
  • Example of Variable:
variable "filename" {
default = ""
}
  • Example of Output:
output "instance_ip_addr" {
  value = aws_instance.example.public_ip
}

Task-1:Create a local file using Terraform

  • Step 1: Create a main.tf file and write the code for creating a local file using Terraform.

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

    Screenshot from 2023-09-19 09-56-17

    • Step 2: Create a variables.tf file and write the code for creating a variable.
    variable "filename" {
        default = "/home/ubuntu/Documents/terraform/day2/demo.txt"
    }
    variable "content" {
        default = "This is a sample file.
    }

This initializes terraform in the folder. It installs all the plugins and providers required to run the Terraform file.


     terraform init

This command shows what are the changes to be implemented through terraform.


     terraform plan

Finally, the changes are made using this command.


     terraform apply

we can see a file is created with the content that we had given in the variable file

Task-02: Data Types in Terraform

Data Types in Terraform.

What are the Data Types in Terraform?

Terraform supports several data types:

  1. String: Holds text or characters.

  2. Number: Stores numeric values (integers or floats).

  3. Bool: Represents boolean values (true or false).

  4. List: An ordered collection of values.

  5. Map: Holds key-value pairs.

  6. Object (Complex Type): Complex data type for structured data.

What is Map in Terraform?

  • In Terraform, a map is a data structure that stores key-value pairs. It allows you to organize and store data efficiently.

  • Example of Map in Terraform:

variable "example_map" {
  type = map(string)
  default = {
    key1 = "value1"
    key2 = "value2"
    key3 = "value3"
  }
}
  • Step 1: let's create a main.tf file and write the code for creating a map in Terraform.
provider "aws"{
    region = "ap-south-1"
}
resource "local_file" "example3"{
    filename = "/home/rohit/Documents/terraform/day3/devops.txt"
    content = var.file_contents["statement1"]
}
resource "local_file" "example4"{
    filename = "/home/rohit/Documents/terraform/day3/devops+cloud.txt"
    content = var.file_contents["statement2"]
}
  • Step 2: Let's create a variables.tf file and write the code for creating a map in Terraform.
  •   variable "filename"{
          default = "/home/ubuntu/Documents/terraform/demo/demo.txt"
      }
      varible "content"{
          default = "Hello World ..."
      }
      variable "file_contents"{
          type = map
          default = {
              "statement1" = "This is a sample file."
              "statement2" = "This is a sample file for Devops and Cloud."
          }
      }
    
  • Now run the command terraform init to initialize the Terraform.
terraform init
  • Now run the command terraform apply to create a map in Terraform.
terraform apply

Now verify the map using the command cat

Task-03:

What is a list in Terraform?

  • In Terraform, a list is a data type that represents an ordered collection of values. Lists maintain the order of elements and can contain values of the same or different data types.

  • They are declared using square brackets and accessed by index. Lists are versatile for storing and working with multiple values within a single variable.

variable "example_list" {
  type    = list
  default = ["item1", "item2", "item3"]
}
  • Let's create a variables.tf file and write the code for creating a list in Terraform.
variable "file_contents"{
    type = map
    default = {
        "statement1" = "This is a sample file."
        "statement2" = "This is a sample file for Devops and Cloud."
    }
}
variable "file_list_type"{
    type = list
    default = [
        "/home/Akshay/Documents/terraform/day3/devops.txt",
        "/home/Akshay/Documents/terraform/day3/devops+cloud.txt"
    ]
}
  • At last, run the command terraform apply to create a list in Terraform.
  • What is an Object in Terraform?

    • In Terraform, an "object" is a complex a data structure that organizes related data using key-value pairs. It allows you to represent structured data with various attributes or properties, each with its own data type.

    • Objects are useful for organizing and managing hierarchical or structured information within Terraform configurations.

    • Step 1: let's create a main.tf file and write the code for creating an object in Terraform.

    provider "aws" {
      region = "ap-south-1"
    }
    output "aws_ec2_object" {
      value = var.aws_ec2_object
    }
  • Let's create a variables.tf file and write the code for creating an object in Terraform.
    variable "aws_ec2_object" {
      type = object({
        name     = string
        instance = number
        keys     = list(string)
        ami      = string
      })
      default = {
        name     = "MyEC2"
        instance = 1
        keys     = ["apache.pem"]
        ami      = "ami-0f5ee92e2d63afc18"
      }
    }

What is Set in Terraform?

  • In Terraform, a "set" typically refers to creating a distinct collection of resources or modules based on conditions or input data. This is achieved using the count or for_each arguments to dynamically manage instances within Terraform configurations.

  • While Terraform doesn't have a specific "set" data type, it allows you to achieve similar functionality by selectively creating or managing resources. Keep in mind that Terraform capabilities may have evolved since my last update, so consult the latest documentation for any changes.

  • Step 1: let's create a main.tf file and write the code for creating a set in Terraform.

    provider "aws" {
      region = "ap-south-1"
    }
    output "sercurity_groups" {
      value = var.sercurity_groups
    }

After running the terraform init command run terraform plan to create a Set in Terraform.

    • Use terraform refresh to refresh the state by your configuration file, and reload the variables.
    terraform refresh

Did you find this article valuable?

Support Akshay Phadke by becoming a sponsor. Any amount is appreciated!