0
0
Terraformcloud~5 mins

Why variables make configurations reusable in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
When you write infrastructure code, you want to use it many times without changing the main file. Variables help by letting you change values easily without rewriting the whole code. This saves time and reduces mistakes.
When you want to create the same infrastructure in different environments like testing and production with small changes.
When you need to share your infrastructure code with teammates who might use different settings.
When you want to avoid repeating the same values in many places in your code.
When you want to quickly update settings like server size or region without editing many files.
When you want to keep sensitive information like passwords separate from your main code.
Config File - main.tf
main.tf
variable "region" {
  description = "The cloud region to deploy resources"
  type        = string
  default     = "us-east-1"
}

variable "instance_type" {
  description = "Type of the compute instance"
  type        = string
  default     = "t2.micro"
}

provider "aws" {
  region = var.region
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
  tags = {
    Name = "example-instance"
  }
}

This file defines two variables: region and instance_type. They let you change where and what kind of server you create without changing the main code.

The provider block uses the region variable to decide where to deploy.

The aws_instance resource uses the instance_type variable to set the server size.

Commands
This command sets up Terraform in the current folder. It downloads needed plugins and prepares to run your code.
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 before making any changes. It uses the variables with their default values.
Terminal
terraform plan
Expected OutputExpected
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" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
This command creates the resources using the variables. The -auto-approve flag skips asking for confirmation.
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 - Skip confirmation prompt before applying changes
This command deletes the created resources to clean up. The -auto-approve flag skips confirmation.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_instance.example: Destroying... aws_instance.example: Still destroying... [10s elapsed] aws_instance.example: Destruction complete after 12s Destroy complete! Resources: 1 destroyed.
-auto-approve - Skip confirmation prompt before destroying resources
Key Concept

If you remember nothing else from this pattern, remember: variables let you change important settings without rewriting your whole infrastructure code.

Common Mistakes
Hardcoding values directly in resource blocks instead of using variables
This makes the code less flexible and harder to reuse in different situations.
Define variables for values that might change and reference them in your resources.
Not providing default values for variables and not passing values when running Terraform
Terraform will ask for input every time or fail if it cannot get a value.
Either set sensible default values in the variable block or pass values using command line or files.
Using variables but forgetting to reference them with the correct syntax (var.variable_name)
Terraform will not recognize the variable and throw an error.
Always use var.variable_name to access variable values inside your configuration.
Summary
Use variables to make your Terraform code flexible and reusable.
Initialize Terraform with 'terraform init' before planning or applying.
Use 'terraform plan' to see what changes will happen with current variable values.
Apply changes with 'terraform apply' and clean up with 'terraform destroy'.