0
0
Terraformcloud~3 mins

Why Variable declaration syntax in Terraform? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could change your entire cloud setup by editing just one line?

The Scenario

Imagine you are setting up cloud resources by typing every detail directly into your configuration files without any placeholders or reusable parts.

Every time you want to change a value, you have to find and edit it in many places manually.

The Problem

This manual way is slow and risky because you might miss some places or make typos.

It's hard to keep track of what values you used and to update them consistently.

The Solution

Using variable declaration syntax lets you define values once and reuse them everywhere.

This makes your setup cleaner, easier to update, and less error-prone.

Before vs After
Before
resource "aws_instance" "example" {
  instance_type = "t2.micro"
  ami           = "ami-123456"
}
After
variable "instance_type" {
  default = "t2.micro"
}

resource "aws_instance" "example" {
  instance_type = var.instance_type
  ami           = "ami-123456"
}
What It Enables

You can quickly change your infrastructure's behavior by updating variables in one place, making your cloud setup flexible and maintainable.

Real Life Example

When launching multiple servers, you can declare a variable for the server size and reuse it, so changing the size for all servers is just one edit.

Key Takeaways

Manual value changes are slow and error-prone.

Variables let you define values once and reuse them.

This makes cloud configurations easier to manage and update.