Terraform Variable

  • Variable helps in centralize and manage values in the configuration.
  • The variable block consist of
    • Type
    • Description
    • Default values.
  • We can create a file variable.tf or default file name terraform.tfvars. We can also specify flag for variable files.
  • For any other file name used to define variable, use the command flag, –var-file. You can specify more than one file, but they will be executed in sequence.
  • All environment variables are defined using prefix TF_VAR_<variable name>
  • Terraform as a built in variable called as PATH it’s basically used in modules.
  • We can have below variable types in Terraform
    • String – Can be Boolean True or False or simple string
    • Map – A collection type (Associate array or Hash Table, similar to dictonary of Python)
    • List – A collection type (List of item, starting with index Zero).
  • We can call variables four ways,Through command prompt
    • Environment variables
    • Default variables
    • Variable defined in files
  • Variables have name and optional parameters,
    • Type – if type is omitted then terraform consider it as by default string.
    • Default
    • Description
  • Variables definition,
  • To call the variable in terraform main file. We need to use the keyword var as prefix
  • Syntax : var.<variable_name>
  • Calling the variables in main terraform file,