Bird
Raised Fist0
Terraformcloud~5 mins

Default values in Terraform - Commands & Configuration

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
Introduction
Sometimes you want to set a value in your infrastructure code but also want to have a backup value if nothing is given. Default values let you do this so your code works smoothly even if you forget to provide some inputs.
When you want to create a reusable module that can work with or without specific input values.
When you want to avoid errors caused by missing input variables in your Terraform configuration.
When you want to provide a common setting but allow users to override it if needed.
When you want to simplify your Terraform commands by not requiring all variables every time.
When you want to ensure your infrastructure has safe fallback values to prevent misconfiguration.
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 = "Type of EC2 instance"
  type        = string
  default     = "t2.micro"
}

This file defines two input variables for Terraform.

region: The AWS region where resources will be created. It has a default value of us-east-1, so if you don't specify it, Terraform uses this region.

instance_type: The type of EC2 instance to launch. It defaults to t2.micro, a small and inexpensive instance type.

Using default means these values are optional when running Terraform.

Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares the environment.
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 based on the current configuration and default values. It helps you verify before applying changes.
Terminal
terraform plan
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 = "t2.micro" } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes to create or update infrastructure. The flag skips the confirmation prompt for faster execution.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
→
-auto-approve - Automatically approve the apply without asking for confirmation
Key Concept

If you remember nothing else from this pattern, remember: default values let your Terraform code work even when inputs are missing by providing safe fallback settings.

Common Mistakes
Not setting a default value for a variable and then running Terraform without providing that variable.
Terraform will fail with an error because it requires that variable but has no value to use.
Always set a default value if the variable is optional or provide the variable value when running Terraform.
Setting a default value that does not match the expected type of the variable.
Terraform will throw a type error and refuse to run the plan or apply.
Make sure the default value matches the declared variable type exactly.
Summary
Define variables with default values in a variables.tf file to make inputs optional.
Run terraform init to prepare the environment and download providers.
Use terraform plan to see what Terraform will do using default or provided values.
Apply changes with terraform apply, optionally skipping confirmation with -auto-approve.

Practice

(1/5)
1. What is the purpose of setting a default value for a variable in Terraform?
easy
A. To make the variable mandatory without a fallback
B. To provide a fallback value if no other value is given
C. To disable the variable completely
D. To automatically generate a random value

Solution

  1. Step 1: Understand variable defaults in Terraform

    Default values act as fallback values when no input is provided by the user.
  2. Step 2: Identify the purpose of default values

    They make variables optional and provide a safe, predictable value.
  3. Final Answer:

    To provide a fallback value if no other value is given -> Option B
  4. Quick Check:

    Default value = fallback [OK]
Hint: Default means fallback if no input given [OK]
Common Mistakes:
  • Thinking default makes variable mandatory
  • Assuming default disables the variable
  • Believing default auto-generates random values
2. Which of the following is the correct syntax to set a default value for a variable named region in Terraform?
easy
A. variable "region" { default = "us-east-1" }
B. variable region { default: "us-east-1" }
C. var region = default "us-east-1"
D. variable "region" = "us-east-1"

Solution

  1. Step 1: Recall Terraform variable block syntax

    Variables are declared with the keyword variable and a name in quotes.
  2. Step 2: Check correct default assignment

    The default value is set inside the block with default = "value".
  3. Final Answer:

    variable "region" { default = "us-east-1" } -> Option A
  4. Quick Check:

    Correct syntax uses variable block and default = value [OK]
Hint: Use variable "name" { default = "value" } [OK]
Common Mistakes:
  • Missing quotes around variable name
  • Using colon instead of equals for default
  • Assigning default outside variable block
3. Given this variable declaration:
variable "instance_type" {
  default = "t2.micro"
}

What will be the value of var.instance_type if no override is provided?
medium
A. null
B. Error: variable not set
C. empty string ""
D. "t2.micro"

Solution

  1. Step 1: Identify the default value in the variable block

    The variable instance_type has a default set to "t2.micro".
  2. Step 2: Understand behavior when no override is given

    If no value is provided, Terraform uses the default value automatically.
  3. Final Answer:

    "t2.micro" -> Option D
  4. Quick Check:

    Default used if no override [OK]
Hint: No override? Use default value [OK]
Common Mistakes:
  • Assuming null if no value given
  • Expecting error without override
  • Thinking default is empty string
4. You wrote this variable block:
variable "env" {
  default: "production"
}

Why does Terraform give a syntax error?
medium
A. Because default uses colon instead of equals sign
B. Because variable name must not be in quotes
C. Because default value must be a number
D. Because variable blocks cannot have defaults

Solution

  1. Step 1: Check the syntax for default assignment

    Terraform requires = to assign default values, not a colon.
  2. Step 2: Identify the cause of syntax error

    Using default: "production" causes a syntax error because colon is invalid here.
  3. Final Answer:

    Because default uses colon instead of equals sign -> Option A
  4. Quick Check:

    Use = not : for default [OK]
Hint: Use = for default, not colon : [OK]
Common Mistakes:
  • Using colon instead of equals for default
  • Removing quotes from variable name
  • Thinking default must be numeric
5. You want to create a variable availability_zones with a default list of zones ["us-east-1a", "us-east-1b"]. Which is the correct way to declare it in Terraform?
hard
A. variable "availability_zones" { default = "us-east-1a, us-east-1b" }
B. variable availability_zones = ["us-east-1a", "us-east-1b"]
C. variable "availability_zones" { type = list(string) default = ["us-east-1a", "us-east-1b"] }
D. variable "availability_zones" { type = string default = ["us-east-1a", "us-east-1b"] }

Solution

  1. Step 1: Define variable with correct type for list of strings

    Use type = list(string) to specify a list of strings.
  2. Step 2: Assign default as a list of strings

    Set default = ["us-east-1a", "us-east-1b"] inside the variable block.
  3. Final Answer:

    variable "availability_zones" { type = list(string) default = ["us-east-1a", "us-east-1b"] } -> Option C
  4. Quick Check:

    List type with list default [OK]
Hint: Use type list(string) and default list [OK]
Common Mistakes:
  • Omitting type for list variables
  • Assigning list to string type
  • Using string with commas instead of list