Terraform Variables | HVTools.Net - The Legend of Hanuman

Terraform Variables | HVTools.Net


Similar to all other programming languages, Terraform also makes use of variables for dynamic coding.

You can have a dedicated Terraform variables file with extension .TFVARS within your working directory/folder and this will automatically be referenced in your main Terraform code file, so you can call directly and/or interpolate.

There are several different types of variables in Terraform;

  • String; Name objects or any methods requiring simple text.
  • Map; Defining Azure location/region methods.
  • List; Defining Azure Subnets or IP address methods. Lists are essentially arrays, for those familiar with programming syntax, ie. [ ].
  • Boolean; Defining True/False methods. Bear in mind, in Terraform type is still defined as String.

String

variable "server_name {
 default = "nethugo-server"
}

Map

variable "locations" {
 type = "map"
 default = {
  location1 = "Australia Southeast"
  location2 = "westus2"
  }
}

List

variable "subnets" {
 type = "list"
 default = ["10.0.1.10", "10.0.1.11"]
}

Boolean

variable "status" {
 type = "string"
 default = false
}

Calling Variables

We can call above variables with the following. Ensure you include quotes.

  • String; “${var.server_name}”
  • Map: “${var.locations[“location1″]}”
    • Output: Australia Southeast
  • List: “${var.subnets[0]}”
  • Boolean: “${var.status}”

Examples

.TFVARS variables

tfvars 1

Initialising TFVARS variables in Main .TF file

initialising tfvars

Calling TFVARS variables in Main .TF file

calling tfvars 1


Share this content:

I am a passionate blogger with extensive experience in web design. As a seasoned YouTube SEO expert, I have helped numerous creators optimize their content for maximum visibility.

Leave a Comment