DEV Community

Adarsh Gupta
Adarsh Gupta

Posted on

Terraform Variables in AWS – Input vs Output vs Local Variables

Why Use Variables in Terraform?

Hardcoding values across multiple files makes updates risky and inefficient. Variables eliminate this problem.
You define a value once, reference it wherever needed, and update it centrally. This keeps your infrastructure code consistent and easier to manage.


Three Types of Terraform Variables

1. Input Variables (variables.tf)

Input variables enable customization of Terraform configurations. They behave like parameters in a function—values can be passed from CLI, tfvars files, or environment variables.

Example:

variable "environment" {
  description = "Environment name"
  type        = string
  default     = "staging"
}
Enter fullscreen mode Exit fullscreen mode

These variables are later referenced using the var. prefix.


2. Local Variables (locals.tf)

Local variables store computed or reusable values within Terraform. They help avoid repetition and simplify expressions.

Example:

locals {
  common_tags = {
    Environment = var.environment
    Project     = "Terraform-Demo"
  }

  full_bucket_name = "${var.environment}-${var.bucket_name}-${random_string.suffix.result}"
}
Enter fullscreen mode Exit fullscreen mode

These values are internal to Terraform and cannot be passed from outside.


3. Output Variables (output.tf)

Output variables return values after resources are created. They behave like function return values and are useful during automation pipelines or when passing data between Terraform modules.

Example:

output "bucket_name" {
  description = "Name of the S3 bucket"
  value       = aws_s3_bucket.demo.bucket
}
Enter fullscreen mode Exit fullscreen mode

The values appear only after running terraform apply.


Input Variables in Detail

Structure of an Input Variable

variable "variable_name" {
  description = "Purpose of the variable"
  type        = string
  default     = "default_value"  # Optional
}
Enter fullscreen mode Exit fullscreen mode

Defining Input Variables

variables.tf

variable "environment" {
  description = "Environment name"
  type        = string
  default     = "staging"
}

variable "bucket_name" {
  description = "S3 bucket name"
  type        = string
  default     = "my-terraform-bucket"
}
Enter fullscreen mode Exit fullscreen mode

Using Input Variables

main.tf

resource "aws_s3_bucket" "demo" {
  bucket = var.bucket_name

  tags = {
    Environment = var.environment
  }
}
Enter fullscreen mode Exit fullscreen mode

How to Provide Values for Input Variables

1. Default Values

If no other values are supplied, Terraform uses defaults defined in variables.tf.

2. terraform.tfvars

Automatically loaded when present.

environment = "demo"
bucket_name = "terraform-demo-bucket"
Enter fullscreen mode Exit fullscreen mode

3. Command Line

terraform plan -var="environment=production"
Enter fullscreen mode Exit fullscreen mode

4. Environment Variables

export TF_VAR_environment="development"
terraform plan
Enter fullscreen mode Exit fullscreen mode

Output Variables in Detail

Structure

output "output_name" {
  description = "Details"
  value       = resource.resource_name.attribute
}
Enter fullscreen mode Exit fullscreen mode

Useful Outputs

output "bucket_name" {
  value = aws_s3_bucket.demo.bucket
}

output "bucket_arn" {
  value = aws_s3_bucket.demo.arn
}

output "environment" {
  value = var.environment
}

output "tags" {
  value = local.common_tags
}
Enter fullscreen mode Exit fullscreen mode

Viewing Outputs

terraform output
terraform output bucket_name
terraform output -json
Enter fullscreen mode Exit fullscreen mode

What This Configuration Builds

This example deploys a single S3 bucket and demonstrates:

  • Input variables controlling bucket name and environment
  • Local variables computing final names and tags
  • Output variables exposing bucket name, ARN, and environment after creation

Variable Precedence (Lowest → Highest)

  1. Default values in variables.tf
  2. Environment variables (TF_VAR_)
  3. terraform.tfvars
  4. terraform.tfvars.json
  5. CLI flags: -var or -var-file

This ensures complete control over variable inputs depending on your workflow.


Testing Variable Precedence

Hide terraform.tfvars and test defaults

mv terraform.tfvars terraform.tfvars.backup
terraform plan
Enter fullscreen mode Exit fullscreen mode

Restore tfvars

mv terraform.tfvars.backup terraform.tfvars
terraform plan
Enter fullscreen mode Exit fullscreen mode

Command line override

terraform plan -var="environment=production"
Enter fullscreen mode Exit fullscreen mode

Environment variable

export TF_VAR_environment="stage"
terraform plan
Enter fullscreen mode Exit fullscreen mode

Specific tfvars file

terraform plan -var-file="dev.tfvars"
Enter fullscreen mode Exit fullscreen mode

Recommended File Structure

├── main.tf
├── variables.tf
├── locals.tf
├── output.tf
├── provider.tf
├── terraform.tfvars
└── README.md
Enter fullscreen mode Exit fullscreen mode

Practical Examples

Example 1: Testing Input Values

mv terraform.tfvars terraform.tfvars.backup
terraform plan

mv terraform.tfvars.backup terraform.tfvars
terraform plan

terraform plan -var="environment=test" -var="bucket_name=my-test-bucket"
Enter fullscreen mode Exit fullscreen mode

Example 2: Viewing All Variable Types

terraform apply -auto-approve
terraform output
echo "Environment: $(terraform output -raw environment)"
echo "Full bucket name: $(terraform output -raw bucket_name)"
Enter fullscreen mode Exit fullscreen mode

Example 3: Precedence in Action

terraform plan | grep Environment

export TF_VAR_environment="from-env-var"
terraform plan | grep Environment

terraform plan -var="environment=from-command-line" | grep Environment
Enter fullscreen mode Exit fullscreen mode

Useful Commands

terraform init
terraform plan
terraform plan -var="environment=test"
terraform plan -var-file="dev.tfvars"
terraform apply
terraform output
terraform destroy
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • Input variables parameterize configurations and increase flexibility.
  • Local variables compute reusable values inside Terraform.
  • Output variables expose important resource information after deployment.
  • Precedence rules ensure predictable overrides during automation and testing.

Reference Video

This video demonstrates the same concept visually and guided this learning:
https://youtu.be/V-2yC39BONc?si=DgCJjBKUKvqFjFq7


Top comments (0)