Terraform Variables

Terraform Variables

What is Terraform Variable

  • Variables are placeholders for values that can be used within Terraform configurations. They allow for flexibility and customization by enabling users to pass values from external sources or prompt user input. Variables can be defined in Terraform configuration files or external input files.

  • To define a variable in Terraform, you can use the “variable” block in your configuration file or create a separate “.tfvars” file. Here’s an example of defining a variable in a configuration file:

  variable "region" {
    description = "The AWS region"
    type        = string
    default     = "us-east-1"
  }
  • In this example, we define a variable named “region” with a description, type, and default value. The variable is of type string and has a default value of “us-east-1”. You can reference this variable later in your configuration by using the syntax “${var.region}”.

  • We can create a variable.tf file which will hold all the variables. These variables can be accessed by the var object in main.tf

Task-01

Create a local file using Terraform

  • Create a variable.tf, and add details regarding file creation by providing the new file complete path.
  variable "filename" {
      default = "/home/ubuntu/terraform/terraform-variables/demo-var.txt"
  }

  variable "content" {
     default = "This is coming from a variable which was updated"
  }

  • Create a main.tf file where we will access the variables that have defined before.
  resource "local_file" "devops" {
  filename = var.filename
  content = var.content
  }

  • Initializes a new terraform working directory.
  terraform init

  • Generates an execution plan by using terraform plan command that shows what actions Terraform will take to reach the desired state specified in the configuration file.

  • Once terraform apply command runs, Terraform will create a file called demo-var.txt in the local directory with the content specified in the content variable that we had mentioned before.

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

terraform refresh

  • The “terraform refresh" command in Terraform compares the current state of the infrastructure with the state defined in the Terraform configuration files. It updates the Terraform state file to reflect any changes made outside of Terraform, ensuring accurate tracking of resource status and attributes.

  • In short, we can say this command is used to refresh the state of your configuration file and reloads the variables.

Task-02

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

  • Put proper screenshots of the outputs

  • Use terraform refresh

  • To refresh the state of your configuration file, reloads the variables

Data Types in Terraform

Map

A “map” variable is a data type used to store key-value pairs identified by a string label. It allows you to define a collection of values associated with unique keys, providing a flexible way to configure and manage resources in your infrastructure-as-code deployments.

  • Inside variable.tf create a variable type map and put content inside it.
  variable "filename" {
      default = "/home/ubuntu/terraform/terraform-variables/terra-dt/demo-map1.txt"
  }

  variable "content" {
     default = "This is coming from a variable which was updated"
  }

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

  • In this example, we use the file_contents variable to set the content parameter of local_file resource.

  • Now from the main.tf access the variables that have been created before inside variable.tf

  resource "local_file" "devops" {
      filename = var.filename
      content = var.file_contents["statement1"]
  }

  resource "local_file" "devops_var" {
      filename = "/home/ubuntu/terraform/terraform-variables/terra-dt/demo-map2.txt"
      content = var.file_contents["statement2"]
  }

  • Now initialize the terraform in the current directory using terraform init command.

  • Once terraform is initialized and all dependencies are done, execute the terraform plan.

  • As the terraform plan satisfies all the steps, execute the terraform apply command.

  • We can verify the file content in the directory.

List:

A list is a data type used to store ordered collections of values of the same type. It allows you to define and manage sequences of values, providing a convenient way to handle multiple elements within a single variable in your infrastructure-as-code deployments.

Here’s an example:

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

  • In this example, we define a variable named “file_list” as a list of strings with a default value that contains the file path. This variable can be used in your Terraform configuration to reference the file paths defined in the list.
  resource "local_file" "devops" {
      filename = var.file_list[0]
      content = var.file_contents["statement1"]
  }

  resource "local_file" "devops_var" {
      filename = var.file_list[1]
      content = var.file_contents["statement2"]
  }

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

  • Execute the terraform plan command.

  • Execute the terraform apply the command.

  • Once terraform apply is completed, check whether 2 files are created with different content.

Object

  • An object is a structural type that is used to store structured data with named attributes and each has its type.

  • Objects are typically used to group related attributes together and provide a structured way to organize and manage data within a configuration. Object data types in Terraform are represented using maps, which are key-value pairs.

Here’s an example:

variable "example_object" {
  type = map(object({
    name  = string
    age   = number
    email = string
  }))
  default = {
    user1 = {
      name  = "John Doe"
      age   = 30
      email = "john.doe@example.com"
    }
    user2 = {
      name  = "Jane Smith"
      age   = 25
      email = "jane.smith@example.com"
    }
  }
}

Set:

A set is an unordered collection of unique values of the same type. To define a set variable, use the set type and specify the type of elements in the set.

Here’s an example:

variable "security_groups" {
  type    = set
  default = ["sg-0692hl0ccdac0f320", "sg-04fg5ca004127f39e"]
}

variable "set" {
  type = set(string)
  default = ["foo","bar",]
}

output "set" {
  value = var.set
}

output "set_first_element" {
  value = var.set[0]
}

In this example, we define a variable named “security_groups” as a set of strings with a default value.