Bird
Raised Fist0
Terraformcloud~5 mins

Module inputs (variables) in Terraform - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you build reusable parts in Terraform called modules, you need a way to give them information. Module inputs, called variables, let you send values into these modules so they can work with different settings without changing the code inside.
When you want to reuse the same Terraform code for different environments like testing and production with different settings.
When you need to pass configuration details like region, instance size, or network settings into a module.
When you want to keep your Terraform code clean and flexible by separating fixed code from changeable values.
When you share Terraform modules with others and want them to provide their own inputs easily.
When you want to avoid repeating the same code by using modules with different inputs.
Config File - variables.tf
variables.tf
variable "region" {
  description = "The AWS region to deploy resources"
  type        = string
  default     = "us-east-1"
}

variable "instance_type" {
  description = "The type of EC2 instance to use"
  type        = string
  default     = "t2.micro"
}

variable "environment" {
  description = "The deployment environment"
  type        = string
  default     = "dev"
}

This file defines three input variables for a Terraform module:

  • region: The AWS region where resources will be created, defaulting to us-east-1.
  • instance_type: The size/type of the EC2 instance, defaulting to t2.micro.
  • environment: A label for the deployment environment, defaulting to dev.

These variables allow the module to be flexible and reusable by accepting different values when used.

Commands
This command initializes the Terraform working directory, downloading necessary providers and preparing to run Terraform commands.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will do using the provided variable values to override defaults. It helps verify changes before applying.
Terminal
terraform plan -var='region=us-west-2' -var='instance_type=t3.small' -var='environment=prod'
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t3.small" + tags = { + "Environment" = "prod" } } Plan: 1 to add, 0 to change, 0 to destroy.
→
-var - Override variable values from the command line
This command applies the planned changes using the specified variable values, creating or updating resources without asking for confirmation.
Terminal
terraform apply -var='region=us-west-2' -var='instance_type=t3.small' -var='environment=prod' -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 20s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Skip interactive approval before applying changes
This command shows the outputs defined in the Terraform module, which can include information about created resources.
Terminal
terraform output
Expected OutputExpected
instance_id = "i-0abcd1234efgh5678" instance_public_ip = "54.123.45.67"
Key Concept

If you remember nothing else from this pattern, remember: variables let you send different settings into a Terraform module to reuse the same code with flexibility.

Common Mistakes
Not providing required variable values when running Terraform commands.
Terraform will fail to plan or apply because it lacks necessary input values.
Always provide variable values via -var flags, variable files, or defaults in the variables.tf file.
Defining variables but never using them inside the module resources.
Variables have no effect if not referenced, so the module won't change based on inputs.
Use variables inside resource blocks with syntax like var.variable_name to make them effective.
Using inconsistent variable names or typos when passing values on the command line.
Terraform ignores unknown variables and may use defaults or error, causing unexpected behavior.
Double-check variable names and spellings when passing values with -var flags.
Summary
Define input variables in a variables.tf file to make modules flexible.
Use terraform plan with -var flags to test different input values before applying.
Apply changes with terraform apply and pass variable values to customize resource creation.

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