Terraform
Basics & Fundamentals
- Infrastructure as Code (IaC)
- Declarative Syntax in IaC
- Terraform Configuration Files
- Terraform CLI
- Terraform Init
- Terraform Plan
- Terraform Apply
- Terraform Destroy
Providers & Resources
Variables & Outputs
π 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:
- Default Value (defined inside
variable
block). - Command-Line Flag β
terraform apply -var="region=us-east-1"
. - Variable Definition File (
.tfvars
) βterraform apply -var-file="prod.tfvars"
. - 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
andt2.micro
) are used. - We can override them during runtime.
Run with custom values:
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:
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
- Reusability β Write once, use in multiple environments.
- Scalability β Essential for large IaC projects.
- Automation β Critical in CI/CD pipelines.
- Best Practices β Avoids hardcoding, improves maintainability.
- 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
-
Multi-Environment Deployments
- Same Terraform code can deploy infrastructure in dev, staging, and prod by just changing input variables.
-
Cost Optimization
- Use smaller instances in dev (
t2.micro
), larger in prod (t3.large
).
- Use smaller instances in dev (
-
Global Deployments
- Different regions can be passed dynamically (
us-east-1
,ap-south-1
).
- Different regions can be passed dynamically (
-
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.