Instantiating variables in Terraform is a bit different than how you would do it in other languages.

In HCL, we use a .tf file extension to declare variables and assigned values, and then we assign values in a seperate file or files, with the .tfvars extension.


Defining Variables

In a variables file, like variables.tf, we can define variables with the following construction.

variable "cidr_block" {
  type        = string
  description = "The CIDR block for the subnet"
  default     = "10.0.0.0/16"
}

The file typically contains multiple variable blocks, all of which have a type, often a description, and usually a default.

Writing description can benefit hugely, to reduce bus factor within the platform team.

The variables file will serve as a single reference point used in the configuration, which enhances readability and maintainability.

It is not mandatory to create, but not doing this part, could result in a lot of backtracking further down the line.