Bird
Raised Fist0
Terraformcloud~10 mins

Variable declaration syntax in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Variable declaration syntax
Start
↓
Declare variable block
↓
Specify variable name
↓
Optionally add type
↓
Optionally add default value
↓
Optionally add description
↓
Variable ready for use
↓
End
This flow shows how a variable is declared step-by-step in Terraform, starting from the declaration block to optional details like type, default, and description.
Execution Sample
Terraform
variable "region" {
  type    = string
  default = "us-west-2"
  description = "AWS region to deploy resources"
}
Declares a variable named 'region' of type string with a default value and description.
Process Table
StepActionEvaluationResult
1Start variable declarationvariable block beginsReady to define variable properties
2Set variable namevariable "region" {Variable named 'region' created
3Set typetype = stringVariable type set to string
4Set default valuedefault = "us-west-2"Default value assigned
5Set descriptiondescription = "AWS region to deploy resources"Description added
6End variable blockvariable block closedVariable 'region' fully declared and ready
7Use variablereference as var.regionValue 'us-west-2' used unless overridden
💡 Variable declaration ends after all properties are set and block is closed.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
regionundefinedname='region'type=stringdefault='us-west-2'description='AWS region to deploy resources'fully declared
Key Moments - 3 Insights
Why do we need to specify the variable type?
Specifying the type helps Terraform check that the value used matches expected data, preventing errors. See step 3 in execution_table where type is set.
What happens if we don't provide a default value?
Terraform will require the user to provide a value when applying, otherwise it will error. Step 4 shows setting default is optional.
Can we use the variable before declaring it?
No, the variable must be declared first (step 2) before it can be referenced in the configuration (step 7).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the variable's type after step 3?
Astring
Bnumber
Cboolean
Dundefined
💡 Hint
Check the 'Evaluation' column at step 3 in execution_table.
At which step does the variable get its default value?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for 'default' in the 'Action' column in execution_table.
If we remove the default value, what changes in the variable_tracker?
AThe type will change to 'any'
BThe variable name will change
CThe 'default' column will show 'undefined' or be missing
DThe description will be removed
💡 Hint
Check the 'After Step 4' column in variable_tracker for default value presence.
Concept Snapshot
Terraform variable declaration syntax:
variable "name" {
  type = <type>        # optional, e.g., string, number
  default = <value>    # optional default value
  description = "..." # optional description
}
Variables must be declared before use and help parameterize configurations.
Full Transcript
This visual execution traces how to declare a variable in Terraform. First, the variable block starts. Then the variable name is set. Next, the type is optionally specified to ensure correct data. After that, a default value can be assigned so the variable has a fallback. A description can also be added for clarity. Finally, the variable block ends, making the variable ready to use in the Terraform configuration. The execution table shows each step and its effect on the variable's state. The variable tracker records how the variable's properties change step-by-step. Key moments clarify why type and default matter and the order of declaration. The quiz tests understanding of these steps and their effects. This helps beginners see exactly how Terraform variables are built and used.

Practice

(1/5)
1. What is the purpose of declaring a variable in Terraform?
easy
A. To write comments in the Terraform file
B. To create a new resource in the cloud
C. To define the output of a Terraform plan
D. To store values that can be reused and customized in the configuration

Solution

  1. Step 1: Understand variable role in Terraform

    Variables hold values that can be changed without editing the main code, making it flexible.
  2. Step 2: Compare with other options

    Creating resources, defining outputs, or comments are different Terraform features, not variable declaration.
  3. Final Answer:

    To store values that can be reused and customized in the configuration -> Option D
  4. Quick Check:

    Variables = reusable values [OK]
Hint: Variables hold reusable values to customize code easily [OK]
Common Mistakes:
  • Confusing variables with resources
  • Thinking variables define outputs
  • Using variables as comments
2. Which of the following is the correct syntax to declare a string variable named region with a default value of us-west-1 in Terraform?
easy
A. variable region = "us-west-1"
B. var region = "us-west-1"
C. variable "region" { type = string default = "us-west-1" }
D. variable "region" { default = us-west-1 type = string }

Solution

  1. Step 1: Recall Terraform variable declaration syntax

    Variables use the block format: variable "name" { type = TYPE default = VALUE } with quotes around strings.
  2. Step 2: Check each option

    variable "region" { type = string default = "us-west-1" } matches correct syntax with proper quotes and order. Options B and C are invalid syntax. variable "region" { default = us-west-1 type = string } misses quotes around default value.
  3. Final Answer:

    variable "region" { type = string default = "us-west-1" } -> Option C
  4. Quick Check:

    Correct block syntax with quotes [OK]
Hint: Use variable block with quotes and type before default [OK]
Common Mistakes:
  • Missing quotes around string values
  • Using assignment (=) outside variable block
  • Incorrect order or missing type keyword
3. Given this variable declaration in Terraform:
variable "instance_count" {
  type    = number
  default = 3
}

What will be the value of var.instance_count if no override is provided?
medium
A. 3
B. null
C. 0
D. Error: variable not set

Solution

  1. Step 1: Identify default value in variable block

    The variable instance_count has a default value set to 3.
  2. Step 2: Understand behavior when no override is given

    If no value is provided during plan or apply, Terraform uses the default value.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Default value used when no override = 3 [OK]
Hint: Default value applies if no input given [OK]
Common Mistakes:
  • Assuming default is zero if not set
  • Expecting error without override
  • Confusing null with default
4. Identify the error in this Terraform variable declaration:
variable "enable_logging" {
  type = bool
  default = "true"
}
medium
A. The default value should not be in quotes for bool type
B. The type should be string, not bool
C. The variable name is invalid
D. Missing description field

Solution

  1. Step 1: Check type and default value compatibility

    The variable type is bool, so the default must be a boolean value, not a string.
  2. Step 2: Identify the error in default value

    The default is "true" (a string), but it should be true (boolean without quotes).
  3. Final Answer:

    The default value should not be in quotes for bool type -> Option A
  4. Quick Check:

    Boolean default must be unquoted true/false [OK]
Hint: Boolean defaults must be unquoted true or false [OK]
Common Mistakes:
  • Putting boolean default in quotes
  • Confusing string and bool types
  • Ignoring type mismatch errors
5. You want to declare a variable tags in Terraform that holds a map of strings with a default value of {"env" = "prod", "team" = "devops"}. Which is the correct declaration?
hard
A. variable "tags" { type = map(string) default = { env = prod, team = devops } }
B. variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } }
C. variable "tags" { type = map default = { "env" = "prod", "team" = "devops" } }
D. variable "tags" { type = map(string) default = ["env" = "prod", "team" = "devops"] }

Solution

  1. Step 1: Understand map type syntax in Terraform

    Maps of strings use type = map(string) and string values must be quoted.
  2. Step 2: Check default value format

    Default must be a map with quoted string values inside curly braces, not square brackets.
  3. Step 3: Evaluate options

    variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } correctly uses type = map(string) and default with quoted values. variable "tags" { type = map(string) default = { env = prod, team = devops } } misses quotes on values, variable "tags" { type = map default = { "env" = "prod", "team" = "devops" } } misses string type, variable "tags" { type = map(string) default = ["env" = "prod", "team" = "devops"] } uses square brackets (list) incorrectly.
  4. Final Answer:

    variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } -> Option B
  5. Quick Check:

    Map(string) with quoted string values in braces [OK]
Hint: Use map(string) type and quoted string values in braces [OK]
Common Mistakes:
  • Missing quotes on map values
  • Using map without specifying string type
  • Using list syntax instead of map