Input Variables
Input variables allow you to pass values into Terraform configurations instead of hardcoding values directly inside resource blocks.
Key Topics
- • Variable blocks
- • Default values
- • Descriptions
- • Sensitive values
Learn how to use Terraform variables and outputs to build flexible, reusable, and maintainable Infrastructure as Code configurations.
Terraform Configuration Flow
Define Input Variables
Assign Configuration Values
Reference Variables in Resources
Create Infrastructure
Expose Output Values
Introduction
Terraform variables allow you to make your infrastructure configurations flexible by separating configuration values from the resources that use them.
Instead of hardcoding values such as regions, instance types, environment names, or resource sizes, you can define input variables and provide different values when deploying infrastructure.
Output values allow Terraform to display useful information about the infrastructure it manages, such as resource IDs, IP addresses, DNS names, and other attributes.
Core Concepts
These concepts help you create reusable Terraform configurations that can support multiple environments and infrastructure requirements.
Input variables allow you to pass values into Terraform configurations instead of hardcoding values directly inside resource blocks.
Key Topics
Terraform supports different variable types that help define the expected structure and format of input values.
Key Topics
Variable files allow you to store environment-specific values separately from your main Terraform configuration.
Key Topics
Outputs expose useful information from Terraform-managed infrastructure after resources are created or updated.
Key Topics
Input Variables
Input variables are defined using variable blocks. Each variable can include a name, description, type, and optional default value.
Variables make it possible to reuse the same Terraform configuration with different values for development, staging, and production environments.
Basic Variable Example
variable "region" {
description = "AWS region for infrastructure"
type = string
default = "us-east-1"
}Variable Types
Terraform variable types help define what type of value a configuration expects.
Used for text values such as regions, environment names, instance types, resource names, and identifiers.
Used for numeric values such as instance counts, storage sizes, ports, and other numerical configuration values.
Used for true or false configuration options that enable or disable infrastructure features.
Used for an ordered collection of values such as availability zones, IP addresses, or multiple resource names.
Used for an unordered collection of unique values where duplicate values are not required.
Used for key-value data structures such as tags, labels, environment settings, and configuration values.
Using a Variable in a Resource
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
resource "aws_instance" "web_server" {
ami = "ami-example"
instance_type = var.instance_type
}Variable References
Variables can be referenced inside Terraform configuration using the var.variable_name syntax.
This allows resource configurations to use dynamic values while keeping the infrastructure code reusable and easier to maintain.
Variable Files
Terraform variable files allow you to store values separately from your main Terraform configuration.
This is especially useful when the same infrastructure code is deployed to multiple environments with different configuration requirements.
terraform.tfvars Example
# terraform.tfvars region = "us-east-1" instance_type = "t3.micro" environment = "development"
Terraform Output Example
output "instance_id" {
description = "ID of the created instance"
value = aws_instance.web_server.id
}
output "instance_public_ip" {
description = "Public IP address"
value = aws_instance.web_server.public_ip
}Output Values
Output blocks allow Terraform to display important information about the infrastructure it creates or manages.
Outputs can expose resource IDs, IP addresses, DNS names, storage locations, and other resource attributes that may be useful after deployment.
Why Use Variables
Variables allow the same Terraform configuration to be reused across different environments and infrastructure deployments.
Development, testing, staging, and production environments can use different values without duplicating infrastructure code.
Configuration values can be centralized and managed separately from the resource definitions.
Infrastructure behavior can be changed by supplying different variable values without rewriting the Terraform configuration.
Complete Example
This example demonstrates how input variables can configure resources and how output blocks can expose information after the infrastructure is created.
variable "environment" {
type = string
description = "Deployment environment"
default = "development"
}
variable "instance_type" {
type = string
description = "Instance type"
default = "t2.micro"
}
resource "aws_instance" "web_server" {
ami = "ami-example"
instance_type = var.instance_type
tags = {
Name = "web-server"
Environment = var.environment
}
}
output "server_id" {
value = aws_instance.web_server.id
}
output "server_ip" {
value = aws_instance.web_server.public_ip
}Learning Roadmap
Follow these steps to understand how values move through a Terraform configuration.
Define variable blocks for configuration values that should not be hardcoded.
Specify the expected type of data for your Terraform variables.
Provide default values when a variable should have a fallback configuration.
Store environment-specific configuration values in tfvars files.
Use variable references inside Terraform resources and other configuration blocks.
Expose useful infrastructure information using Terraform output blocks.
Best Practices
Choose descriptive names that clearly explain the purpose of each configuration value.
Specify appropriate types to make Terraform configurations easier to understand and validate.
Document variables and outputs so other users can understand their purpose.
Keep environment-specific configuration values separate from reusable infrastructure code.
Use variables for values that may change between environments or deployments.
Expose useful infrastructure values that are needed after Terraform creates resources.
Continue Learning
Next Step
Practice creating input variables, using different variable types, separating environment configuration, and exposing useful infrastructure information with outputs.