Terraform – Infrastructure as Code for Multi-Cloud Deployments

4.8 Stars
Version 1.6
80 MB
Terraform – Infrastructure as Code for Multi-Cloud Deployments

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp that enables users to define and provision data center infrastructure using a declarative configuration language. With Terraform, you can manage resources across hundreds of cloud providers and services, treating infrastructure configuration as version-controlled code.

Unlike imperative approaches that specify step-by-step instructions for creating infrastructure, Terraform uses a declarative approach where you define the desired end state. Terraform then determines the necessary actions to achieve that state, creating, modifying, or destroying resources as needed.

Terraform has become the industry standard for multi-cloud infrastructure management, used by organizations of all sizes to automate cloud provisioning, ensure consistency across environments, and implement GitOps workflows for infrastructure changes.

Key Features and Capabilities

Infrastructure as Code

Terraform configurations are written in HashiCorp Configuration Language (HCL), a human-readable language designed specifically for infrastructure definition. Configurations can be version controlled, reviewed, and shared like any other code, enabling collaborative infrastructure management.

Multi-Cloud Support

Through providers, Terraform supports virtually every major cloud platform and service. AWS, Azure, Google Cloud, Kubernetes, and hundreds of other providers enable unified infrastructure management across diverse environments from a single tool.

State Management

Terraform maintains a state file that maps real-world resources to your configuration, tracking metadata and improving performance for large infrastructures. State can be stored locally or remotely with locking to prevent concurrent modifications.

Resource Graph

Terraform builds a dependency graph of resources, enabling parallel resource creation where possible and ensuring resources are created in the correct order based on dependencies.

Plan and Apply Workflow

The plan phase shows what changes Terraform will make before any modifications occur. This preview enables review and approval workflows, preventing unintended changes to production infrastructure.

Modules

Modules enable reusable, composable infrastructure components. Public modules from the Terraform Registry and private modules accelerate development while ensuring consistency across projects.

System Requirements

Hardware Requirements

Terraform itself requires minimal resources—any system with 256 MB RAM can run Terraform. Resource requirements primarily depend on the size of your state file and the complexity of your configurations.

Operating System Support

Terraform runs on Windows, macOS, Linux, FreeBSD, and Solaris. Binary releases are available for all major platforms, and the tool can also run in containerized environments.

Installation Guide

Installing on Linux

# Ubuntu/Debian - using HashiCorp repository
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

# RHEL/CentOS
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum install terraform

# Manual installation
wget https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip
unzip terraform_1.6.6_linux_amd64.zip
sudo mv terraform /usr/local/bin/

# Verify installation
terraform version

Installing on macOS

# Using Homebrew
brew tap hashicorp/tap
brew install hashicorp/tap/terraform

# Or install directly
brew install terraform

# Verify installation
terraform version

Installing on Windows

# Using Chocolatey
choco install terraform

# Using Scoop
scoop install terraform

# Manual installation
# Download from terraform.io/downloads
# Extract and add to PATH

# Verify
terraform version

Using Docker

# Run Terraform in Docker
docker run --rm -it \
  -v $(pwd):/workspace \
  -w /workspace \
  hashicorp/terraform:latest version

# Run terraform commands
docker run --rm -it \
  -v $(pwd):/workspace \
  -w /workspace \
  hashicorp/terraform:latest init

HCL Configuration Basics

Basic Syntax

# Provider configuration
provider "aws" {
  region = "us-east-1"
}

# Resource definition
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  
  tags = {
    Name = "WebServer"
    Environment = "Production"
  }
}

# Variable definition
variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}

# Using variables
resource "aws_instance" "app" {
  ami           = var.ami_id
  instance_type = var.instance_type
}

# Output definition
output "instance_ip" {
  description = "Public IP of the instance"
  value       = aws_instance.web.public_ip
}

# Data source (read existing resources)
data "aws_ami" "ubuntu" {
  most_recent = true
  
  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
  }
  
  owners = ["099720109477"] # Canonical
}

# Local values
locals {
  common_tags = {
    Project     = "MyProject"
    Environment = var.environment
    ManagedBy   = "Terraform"
  }
}

Essential Terraform Commands

Workflow Commands

# Initialize working directory
terraform init
terraform init -upgrade  # Upgrade providers

# Validate configuration
terraform validate

# Format configuration
terraform fmt
terraform fmt -recursive

# Show execution plan
terraform plan
terraform plan -out=tfplan
terraform plan -var="instance_type=t3.large"

# Apply changes
terraform apply
terraform apply tfplan
terraform apply -auto-approve

# Destroy infrastructure
terraform destroy
terraform destroy -target=aws_instance.web

# Show current state
terraform show
terraform show -json

# List resources in state
terraform state list

# Show specific resource
terraform state show aws_instance.web

# Import existing resources
terraform import aws_instance.web i-1234567890abcdef0

# Refresh state
terraform refresh

# Create execution plan for destroy
terraform plan -destroy

State Management Commands

# Move resource in state
terraform state mv aws_instance.old aws_instance.new

# Remove resource from state (without destroying)
terraform state rm aws_instance.web

# Pull remote state
terraform state pull

# Push local state to remote
terraform state push

# Replace provider in state
terraform state replace-provider hashicorp/aws registry.acme.corp/acme/aws

Variables and Outputs

Variable Types

# variables.tf

# String variable
variable "region" {
  type        = string
  description = "AWS region"
  default     = "us-east-1"
}

# Number variable
variable "instance_count" {
  type        = number
  description = "Number of instances"
  default     = 2
}

# Boolean variable
variable "enable_monitoring" {
  type        = bool
  description = "Enable detailed monitoring"
  default     = true
}

# List variable
variable "availability_zones" {
  type        = list(string)
  description = "List of availability zones"
  default     = ["us-east-1a", "us-east-1b"]
}

# Map variable
variable "instance_tags" {
  type        = map(string)
  description = "Tags to apply to instances"
  default = {
    Environment = "Development"
    Team        = "Engineering"
  }
}

# Object variable
variable "instance_config" {
  type = object({
    instance_type = string
    volume_size   = number
    encrypted     = bool
  })
  default = {
    instance_type = "t3.micro"
    volume_size   = 20
    encrypted     = true
  }
}

# Sensitive variable
variable "database_password" {
  type        = string
  description = "Database password"
  sensitive   = true
}

# Variable validation
variable "environment" {
  type        = string
  description = "Environment name"
  
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}

Setting Variable Values

# terraform.tfvars
region         = "us-west-2"
instance_count = 3
environment    = "prod"

# terraform.tfvars.json
{
  "region": "us-west-2",
  "instance_count": 3
}

# Command line
terraform plan -var="region=eu-west-1"
terraform plan -var-file="production.tfvars"

# Environment variables
export TF_VAR_region="us-west-2"
export TF_VAR_database_password="secret123"

Providers Configuration

AWS Provider

# AWS provider configuration
provider "aws" {
  region = var.region
  
  # Explicit credentials (not recommended)
  # access_key = "your-access-key"
  # secret_key = "your-secret-key"
  
  # Assume role
  assume_role {
    role_arn = "arn:aws:iam::123456789012:role/TerraformRole"
  }
  
  default_tags {
    tags = {
      ManagedBy = "Terraform"
    }
  }
}

# Multiple provider configurations
provider "aws" {
  alias  = "west"
  region = "us-west-2"
}

resource "aws_instance" "west" {
  provider = aws.west
  # ...
}

Azure Provider

provider "azurerm" {
  features {}
  
  subscription_id = var.subscription_id
  tenant_id       = var.tenant_id
}

resource "azurerm_resource_group" "main" {
  name     = "my-resource-group"
  location = "East US"
}

Google Cloud Provider

provider "google" {
  project = var.project_id
  region  = var.region
}

resource "google_compute_instance" "vm" {
  name         = "my-instance"
  machine_type = "e2-medium"
  zone         = "us-central1-a"
  
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  
  network_interface {
    network = "default"
  }
}

Modules

Creating Modules

# modules/vpc/main.tf
variable "vpc_cidr" {
  type = string
}

variable "environment" {
  type = string
}

resource "aws_vpc" "main" {
  cidr_block           = var.vpc_cidr
  enable_dns_hostnames = true
  
  tags = {
    Name        = "${var.environment}-vpc"
    Environment = var.environment
  }
}

resource "aws_subnet" "public" {
  count             = 2
  vpc_id            = aws_vpc.main.id
  cidr_block        = cidrsubnet(var.vpc_cidr, 8, count.index)
  availability_zone = data.aws_availability_zones.available.names[count.index]
}

output "vpc_id" {
  value = aws_vpc.main.id
}

output "public_subnet_ids" {
  value = aws_subnet.public[*].id
}

Using Modules

# main.tf
module "vpc" {
  source = "./modules/vpc"
  
  vpc_cidr    = "10.0.0.0/16"
  environment = var.environment
}

# Using module outputs
resource "aws_instance" "app" {
  subnet_id = module.vpc.public_subnet_ids[0]
  # ...
}

# Module from Terraform Registry
module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"
  
  name = "my-vpc"
  cidr = "10.0.0.0/16"
  
  azs             = ["us-east-1a", "us-east-1b", "us-east-1c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
  
  enable_nat_gateway = true
}

# Module from Git
module "example" {
  source = "git::https://github.com/example/terraform-module.git?ref=v1.0.0"
}

Remote State Configuration

S3 Backend

terraform {
  backend "s3" {
    bucket         = "my-terraform-state"
    key            = "prod/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Azure Backend

terraform {
  backend "azurerm" {
    resource_group_name  = "tfstate-rg"
    storage_account_name = "tfstatestorage"
    container_name       = "tfstate"
    key                  = "prod.terraform.tfstate"
  }
}

Terraform Cloud

terraform {
  cloud {
    organization = "my-org"
    
    workspaces {
      name = "my-workspace"
    }
  }
}

Best Practices

Project Structure

project/
??? main.tf           # Main configuration
??? variables.tf      # Variable definitions
??? outputs.tf        # Output definitions
??? providers.tf      # Provider configurations
??? versions.tf       # Version constraints
??? terraform.tfvars  # Variable values
??? modules/
?   ??? vpc/
?   ??? compute/
?   ??? database/
??? environments/
    ??? dev/
    ??? staging/
    ??? prod/

Version Constraints

# versions.tf
terraform {
  required_version = ">= 1.0.0"
  
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

Conclusion

Terraform provides a powerful, vendor-agnostic approach to infrastructure management that scales from simple deployments to complex multi-cloud architectures. Its declarative language, state management, and extensive provider ecosystem make it an essential tool for modern infrastructure automation.

Whether managing cloud resources, Kubernetes clusters, or SaaS services, Terraform enables consistent, repeatable, and auditable infrastructure provisioning.

Developer: HashiCorp

Download Options

Download Terraform – Infrastructure as Code for Multi-Cloud Deployments

Version 1.6

File Size: 80 MB

Download Now
Safe & Secure

Verified and scanned for viruses

Regular Updates

Always get the latest version

24/7 Support

Help available when you need it