TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Terraform/Variables and Outputs
Terraform Fundamentals

Terraform Variables and Outputs

Learn how to use Terraform variables and outputs to build flexible, reusable, and maintainable Infrastructure as Code configurations.

Level: Beginner
Topic: Variables & Outputs

Terraform Configuration Flow

Variables → Resources → Outputs

Define Input Variables

Assign Configuration Values

Reference Variables in Resources

Create Infrastructure

Expose Output Values

Introduction

Build flexible Terraform configurations

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

Understanding Terraform variables and outputs

These concepts help you create reusable Terraform configurations that can support multiple environments and infrastructure requirements.

01

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
02

Variable Types

Terraform supports different variable types that help define the expected structure and format of input values.

Key Topics

  • String
  • Number
  • Boolean
  • Collections
03

Variable Files

Variable files allow you to store environment-specific values separately from your main Terraform configuration.

Key Topics

  • terraform.tfvars
  • *.auto.tfvars
  • Environment values
  • Configuration separation
04

Output Values

Outputs expose useful information from Terraform-managed infrastructure after resources are created or updated.

Key Topics

  • Resource attributes
  • Infrastructure details
  • Module outputs
  • Reusable values

Input Variables

Define configurable infrastructure values

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

Define the expected data structure

Terraform variable types help define what type of value a configuration expects.

String

Used for text values such as regions, environment names, instance types, resource names, and identifiers.

Number

Used for numeric values such as instance counts, storage sizes, ports, and other numerical configuration values.

Boolean

Used for true or false configuration options that enable or disable infrastructure features.

List

Used for an ordered collection of values such as availability zones, IP addresses, or multiple resource names.

Set

Used for an unordered collection of unique values where duplicate values are not required.

Map

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

Use variables inside your infrastructure

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

Separate configuration values from infrastructure code

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

Display useful infrastructure information

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

Create reusable Infrastructure as Code

Reusable Configurations

Variables allow the same Terraform configuration to be reused across different environments and infrastructure deployments.

Environment Separation

Development, testing, staging, and production environments can use different values without duplicating infrastructure code.

Better Maintainability

Configuration values can be centralized and managed separately from the resource definitions.

Flexible Infrastructure

Infrastructure behavior can be changed by supplying different variable values without rewriting the Terraform configuration.

Complete Example

Variables, resources, and outputs together

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

Master Terraform variables and outputs

Follow these steps to understand how values move through a Terraform configuration.

Step 1

Create Input Variables

Define variable blocks for configuration values that should not be hardcoded.

Step 2

Add Variable Types

Specify the expected type of data for your Terraform variables.

Step 3

Use Default Values

Provide default values when a variable should have a fallback configuration.

Step 4

Create Variable Files

Store environment-specific configuration values in tfvars files.

Step 5

Reference Variables

Use variable references inside Terraform resources and other configuration blocks.

Step 6

Create Outputs

Expose useful infrastructure information using Terraform output blocks.

Best Practices

Working with variables and outputs

Use Clear Variable Names

Choose descriptive names that clearly explain the purpose of each configuration value.

Define Variable Types

Specify appropriate types to make Terraform configurations easier to understand and validate.

Add Descriptions

Document variables and outputs so other users can understand their purpose.

Use tfvars Files

Keep environment-specific configuration values separate from reusable infrastructure code.

Avoid Hardcoded Values

Use variables for values that may change between environments or deployments.

Output Important Information

Expose useful infrastructure values that are needed after Terraform creates resources.

Next Step

Build reusable Terraform configurations

Practice creating input variables, using different variable types, separating environment configuration, and exposing useful infrastructure information with outputs.