πŸ“˜ Terraform Input Variables: Accept Values Dynamically

When working with Infrastructure as Code (IaC) using Terraform, you often want your infrastructure configurations to be reusable, flexible, and dynamic.

Instead of hardcoding values (like region, instance type, or environment), Terraform allows you to define Input Variables.

πŸ‘‰ Terraform Input Variables = Parameters you pass into your Terraform configurations to make them dynamic and reusable.

This makes your Terraform code:

  • Reusable (same code works for dev, test, prod).
  • Maintainable (easy to update values).
  • Scalable (works with automation pipelines).

πŸ”‘ What Are Terraform Input Variables?

Terraform Input Variables let you:

  • Pass values dynamically into Terraform modules.
  • Avoid hardcoding values like region, AMI IDs, or instance sizes.
  • Share configurations across multiple environments.

They are defined using the variable block in Terraform.

Basic syntax:

variable "variable_name" {
description = "A short explanation of this variable"
type = string
default = "default_value"
}
  • variable_name β†’ Name of the variable.
  • description β†’ Helps document purpose.
  • type β†’ Can be string, number, bool, list, or map.
  • default β†’ Value used if no other value is provided.

βš™οΈ Ways to Provide Input Variable Values

Terraform supports multiple methods to supply variable values:

  1. Default Value (defined inside variable block).
  2. Command-Line Flag β†’ terraform apply -var="region=us-east-1".
  3. Variable Definition File (.tfvars) β†’ terraform apply -var-file="prod.tfvars".
  4. Environment Variables β†’ export TF_VAR_region=us-east-1.

πŸ–₯️ Example Programs

Let’s explore 3 unique examples to understand Input Variables better.


βœ… Example 1: Using Variables for AWS EC2 Instance

Code (main.tf):

variable "region" {
description = "AWS region"
type = string
default = "us-east-1"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
provider "aws" {
region = var.region
}
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
}

Explanation:

  • Region and instance type are dynamic.
  • If no values are passed, defaults (us-east-1 and t2.micro) are used.
  • We can override them during runtime.

Run with custom values:

Terminal window
terraform apply -var="region=us-west-2" -var="instance_type=t3.micro"

βœ… Example 2: Passing Variables via .tfvars File

variables.tf:

variable "app_name" {
description = "Application name"
type = string
}
variable "environment" {
description = "Deployment environment"
type = string
}

main.tf:

resource "aws_s3_bucket" "bucket" {
bucket = "${var.app_name}-${var.environment}-bucket"
acl = "private"
}

prod.tfvars:

app_name = "myapp"
environment = "prod"

Command:

Terminal window
terraform apply -var-file="prod.tfvars"

Explanation:

  • Bucket name changes dynamically based on environment.
  • Helps reuse same code for dev, staging, prod.

βœ… Example 3: Complex Variables (Lists and Maps)

variables.tf:

variable "regions" {
description = "List of AWS regions"
type = list(string)
default = ["us-east-1", "us-west-2"]
}
variable "instance_types" {
description = "Map of environment to instance type"
type = map(string)
default = {
dev = "t2.micro"
prod = "t3.medium"
}
}

main.tf:

provider "aws" {
region = var.regions[0] # Picks first region
}
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_types["prod"]
}

Explanation:

  • Uses list to manage multiple regions.
  • Uses map to select instance types per environment.

🧠 How to Remember Terraform Input Variables (Interview & Exam Prep)

1. Analogy

Think of variables like ordering food at a restaurant:

  • Menu = Terraform code.
  • Your choice (veg/non-veg) = Input variable.
  • Chef cooks = Terraform applies configuration.

2. Memory Trick (VARS)

  • V β†’ Values passed dynamically.
  • A β†’ Avoid hardcoding.
  • R β†’ Reusable infrastructure.
  • S β†’ Supports multiple input methods (CLI, files, env).

3. Interview Shortcut Answer

πŸ‘‰ β€œTerraform Input Variables allow dynamic values in configurations. They make code reusable, flexible, and environment-specific without hardcoding values.”


🎯 Why It’s Important to Learn Terraform Input Variables

  1. Reusability β†’ Write once, use in multiple environments.
  2. Scalability β†’ Essential for large IaC projects.
  3. Automation β†’ Critical in CI/CD pipelines.
  4. Best Practices β†’ Avoids hardcoding, improves maintainability.
  5. Collaboration β†’ Teams can share same Terraform code across regions/environments.

πŸ”₯ Common Interview Questions on Terraform Input Variables

Q1: What are Terraform Input Variables? πŸ‘‰ Parameters that allow you to pass dynamic values into Terraform configurations.

Q2: What types of variables exist in Terraform? πŸ‘‰ string, number, bool, list, map, set, object, tuple.

Q3: How can you pass variable values in Terraform? πŸ‘‰ Default value, CLI flag, .tfvars file, environment variables.

Q4: Difference between Input and Output variables? πŸ‘‰ Input variables β†’ Accept values into Terraform. πŸ‘‰ Output variables β†’ Return values from Terraform after apply.

Q5: What is a .tfvars file used for? πŸ‘‰ To store variable values for specific environments.


🌍 Real-World Use Cases of Input Variables

  1. Multi-Environment Deployments

    • Same Terraform code can deploy infrastructure in dev, staging, and prod by just changing input variables.
  2. Cost Optimization

    • Use smaller instances in dev (t2.micro), larger in prod (t3.large).
  3. Global Deployments

    • Different regions can be passed dynamically (us-east-1, ap-south-1).
  4. Team Collaboration

    • Developers can override values without changing the base code.

πŸ“– Best Practices

  • Always add descriptions for variables.
  • Use sensitive = true for passwords or secrets.
  • Organize values in .tfvars files for different environments.
  • Use default values to avoid errors.
  • Group related variables into objects for better readability.

πŸ† Conclusion

Terraform Input Variables are the backbone of reusability in Infrastructure as Code. By learning how to use them effectively, you:

  • Avoid hardcoding values.
  • Make your code dynamic, flexible, and environment-agnostic.
  • Enable smooth collaboration and automation across teams.

πŸ‘‰ If you are preparing for an interview, just remember: Terraform Input Variables = Dynamic values for reusable infrastructure.

For real-world projects, mastering variables ensures you can deploy infrastructure across multiple environments (dev, staging, prod) with the same Terraform code.