Bird
Raised Fist0
Terraformcloud~10 mins

Module inputs (variables) in Terraform - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Module inputs (variables)
Define variable in module
↓
Call module with input values
↓
Terraform reads input values
↓
Module uses inputs to create resources
↓
Outputs reflect resource state based on inputs
Terraform modules receive input values through variables defined inside the module and passed when calling the module. These inputs control resource creation.
Execution Sample
Terraform
variable "region" {
  type    = string
  default = "us-east-1"
}

module "example" {
  source = "./modules/example"
  region = "us-west-2"
}
Defines a variable 'region' with default, then calls a module passing a different region value.
Process Table
StepActionVariableValueEffect
1Define variable 'region' with defaultregionus-east-1Variable ready with default
2Call module 'example' passing regionregionus-west-2Input overrides default
3Terraform reads module inputregionus-west-2Module uses passed value
4Module creates resources using regionregionus-west-2Resources created in us-west-2
5Module outputs reflect resource stateregionus-west-2Outputs show resources in us-west-2
6Execution ends--All inputs processed, resources created
💡 Module inputs processed; resources created using passed variable values
Status Tracker
VariableDefinedPassed to ModuleUsed in ModuleFinal
regionus-east-1 (default)us-west-2us-west-2us-west-2
Key Moments - 2 Insights
Why does the module use 'us-west-2' instead of the default 'us-east-1'?
Because the module call passes 'region = "us-west-2"', which overrides the variable's default value as shown in execution_table step 2 and 3.
What happens if no value is passed to the module for 'region'?
Terraform uses the variable's default value 'us-east-1' inside the module, as defined in step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what value does the module variable 'region' have?
Aus-east-1
Bundefined
Cus-west-2
Dnull
💡 Hint
Check the 'Value' column at step 3 in the execution_table
At which step does Terraform read the input value passed to the module?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look for the step where Terraform reads module input in the execution_table
If the module call did not pass any value for 'region', what would be the final value used inside the module?
Anull
Bus-east-1
Cus-west-2
Dempty string
💡 Hint
Refer to variable_tracker and key_moments about default values
Concept Snapshot
Terraform modules use variables to accept inputs.
Variables have defaults but can be overridden when calling the module.
Passed values replace defaults inside the module.
Modules use these inputs to create resources.
Outputs reflect the resource state based on inputs.
Full Transcript
In Terraform, modules can accept inputs through variables defined inside them. These variables can have default values. When you call a module, you can pass values to these variables, which override the defaults. Terraform reads these input values during execution and uses them to create resources inside the module. The outputs from the module then reflect the state of these resources based on the inputs provided. This process allows modules to be reusable and configurable by changing input values without modifying the module code.

Practice

(1/5)
1. What is the main purpose of using variables in a Terraform module?
easy
A. To customize module behavior without changing its code
B. To store sensitive data only
C. To define output values from the module
D. To create resources automatically

Solution

  1. Step 1: Understand module customization

    Variables allow users to provide input values to modules, changing how they behave without editing the module code itself.
  2. Step 2: Differentiate variables from other features

    Variables are not for outputs or automatic resource creation; they are inputs to customize modules.
  3. Final Answer:

    To customize module behavior without changing its code -> Option A
  4. Quick Check:

    Module inputs = customization [OK]
Hint: Variables customize modules without code edits [OK]
Common Mistakes:
  • Confusing variables with outputs
  • Thinking variables create resources
  • Assuming variables only store secrets
2. Which of the following is the correct syntax to declare a string variable named region with a default value of us-west-1?
easy
A. variable "region" = "us-west-1"
B. variable region { type = "string" default = us-west-1 }
C. variable "region" { default = us-west-1 type = string }
D. variable "region" { type = string default = "us-west-1" }

Solution

  1. Step 1: Check variable block syntax

    Terraform variables use the syntax: variable "name" { type = string default = "value" } with quotes around strings.
  2. Step 2: Validate variable "region" { type = string default = "us-west-1" }

    correctly uses quotes for the variable name and default string value, and sets type properly.
  3. Final Answer:

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

    Correct variable block syntax = variable "region" { type = string default = "us-west-1" } [OK]
Hint: Use quotes for names and string defaults in variable blocks [OK]
Common Mistakes:
  • Missing quotes around strings
  • Wrong order of attributes
  • Assigning variable directly without block
3. Given this module variable declaration:
variable "instance_count" {
  type    = number
  default = 2
}

And this usage inside the module:
resource "aws_instance" "example" {
  count = var.instance_count
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

What will happen if the module is called without specifying instance_count?
medium
A. Only one instance will be created
B. Two AWS instances will be created
C. An error occurs because instance_count is missing
D. No instances will be created

Solution

  1. Step 1: Understand default variable behavior

    If a variable has a default value and is not overridden, Terraform uses the default.
  2. Step 2: Apply default to resource count

    The count argument uses var.instance_count, which defaults to 2, so two instances are created.
  3. Final Answer:

    Two AWS instances will be created -> Option B
  4. Quick Check:

    Default variable value used = 2 instances [OK]
Hint: Default variable values apply if no input given [OK]
Common Mistakes:
  • Assuming missing variable causes error
  • Thinking default is ignored
  • Confusing count with instance_type
4. You have this variable declaration inside a module:
variable "env" {
  type = string
}

When calling the module, you forget to provide a value for env. What will happen?
medium
A. Terraform will throw an error about missing required variable
B. Terraform will use an empty string as default
C. Terraform will assign null automatically
D. Terraform will use the string "env" as default

Solution

  1. Step 1: Check variable declaration for default

    The variable env has no default value, so it is required.
  2. Step 2: Understand Terraform behavior on missing required variables

    If a required variable is not provided, Terraform throws an error and stops.
  3. Final Answer:

    Terraform will throw an error about missing required variable -> Option A
  4. Quick Check:

    Required variable missing = error [OK]
Hint: No default means variable is required [OK]
Common Mistakes:
  • Assuming empty string or null is automatic default
  • Thinking Terraform guesses a value
  • Confusing variable name with default
5. You want to create a reusable module that accepts a list of subnet IDs as input. Which variable declaration correctly enforces that the input must be a list of strings and has no default value?
hard
A. variable "subnet_ids" { type = string default = [] }
B. variable "subnet_ids" { type = list number default = ["subnet-123"] }
C. variable "subnet_ids" { type = list(string) }
D. variable "subnet_ids" { type = list(string) default = null }

Solution

  1. Step 1: Identify correct type syntax for list of strings

    Terraform uses list(string) to specify a list where each item is a string.
  2. Step 2: Check default value presence

    No default means the variable is required. variable "subnet_ids" { type = list(string) } has no default, so input must be provided.
  3. Final Answer:

    variable "subnet_ids" { type = list(string) } -> Option C
  4. Quick Check:

    List of strings required, no default = variable "subnet_ids" { type = list(string) } [OK]
Hint: Use type = list(string) and omit default to require input [OK]
Common Mistakes:
  • Using wrong type like string instead of list(string)
  • Providing default empty list when input should be required
  • Using invalid syntax for type or default
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard