Day 63 - Terraform Variables

Day 63 - Terraform Variables

Day 63 of 90daysofdevops

Terraform Variables

Terraform allows you to define variables to parameterize your infrastructure code and make it more reusable and configurable. 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/user/terraform/day63/demo-var.txt"
}
variable "content" {
  default = "Hey, This is Shreya. This is a testing of variable"
}

These variables can be accessed by the var object in main.tf

Create a local file using Terraform

  • Create a main.tf and variable.tf file.

  • Run the below command to apply terraform configuration and then the file is created by using a variable.

      terraform init
      terraform plan
      terraform apply
    

Data Types in Terraform

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

In Terraform, variables and resource attributes can have different data types. Here are the commonly used data types in Terraform.

  1. String: Represents a sequence of characters. Strings are enclosed in double quotes ("") or single quotes ('').

    For example: "us-west-2".

  2. Number: Represents numerical values. Numbers can be integers or floating-point values.

    For example: 42, 3.14.

  3. Bool: Represents boolean values, which can be either true or false.

  4. List: Represents an ordered collection of values. Lists are enclosed in square brackets ([]). Each element in the list can be of any data type.

    For example: ["value1", "value2", "value3"].

  5. Map: Represents a collection of key-value pairs. Maps are enclosed in curly braces ({}), and each element in the map is defined as "key" = "value". Both the key and value can be of any data type.

    For example: {"key1" = "value1", "key2" = "value2"}.

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

  6. Set: Represents an unordered collection of unique values. Sets are similar to lists but do not allow duplicate values. Sets are enclosed in curly braces ({}) and use the ["value1", "value2"] syntax.

     variable "ingress_rules" {
       type    = set(object({
         from_port   = number
         to_port     = number
         protocol    = string
         cidr_blocks = list(string)
       }))
       default = [
         {
           from_port   = 22
           to_port     = 22
           protocol    = "tcp"
           cidr_blocks = ["0.0.0.0/0"]
         },
         {
           from_port   = 80
           to_port     = 80
           protocol    = "tcp"
           cidr_blocks = ["0.0.0.0/0"]
         }
       ]
     }
    
     resource "aws_security_group" "example_security_group" {
       ingress {
         from_port   = var.ingress_rules[0].from_port
         to_port     = var.ingress_rules[0].to_port
         protocol    = var.ingress_rules[0].protocol
         cidr_blocks = var.ingress_rules[0].cidr_blocks
       }
    
       ingress {
         from_port   = var.ingress_rules[1].from_port
         to_port     = var.ingress_rules[1].to_port
         protocol    = var.ingress_rules[1].protocol
         cidr_blocks = var.ingress_rules[1].cidr_blocks
       }
    
       vpc_id = aws_vpc.example_vpc.id
     }
    
  7. Object: Represents a complex data structure with multiple attributes. Objects are defined using the . syntax to access nested attributes.

     variable "vpc_config" {
       type = object({
         cidr_block       = string
         instance_tenancy = string
       })
       default = {
         cidr_block       = "10.0.0.0/16"
         instance_tenancy = "default"
       }
     }
    
     resource "aws_vpc" "example_vpc" {
       cidr_block       = var.vpc_config.cidr_block
       instance_tenancy = var.vpc_config.instance_tenancy
     }
    

Thank you for reading!!
~Shreya Gupta

Great initiative by the #trainwithshubham community. Thank you Shubham Londhe

#devops #90daysofdevops #terraform #terraformvariables