Terraform Variables

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.


variable "server_name {
 default = "nethugo-server"


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


variable "subnets" {
 type = "list"
 default = ["", ""]


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

Calling Variables

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

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


.TFVARS variables

Initialising TFVARS variables in Main .TF file

Calling TFVARS variables in Main .TF file