0
0
Terraformcloud~5 mins

Default values in Terraform - Commands & Configuration

Choose your learning style9 modes available
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.