Skip to content

Terraform_introduction

Terraform is an Infrastructure as Code (IaC) tool by HashiCorp that allows you to define and provision cloud infrastructure using declarative configuration files.

Infrastructure as Code is the practice of managing infrastructure through machine-readable definition files rather than manual processes.

┌─────────────────────────────────────────────────────────────────────────────┐
│ Infrastructure as Code Concept │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Traditional Approach: IaC Approach: │
│ ┌────────────────┐ ┌────────────────┐ │
│ │ Click in UI │ │ Write Code │ │
│ │ │ │ │ │ │ │
│ │ ▼ │ │ ▼ │ │
│ │ Manual Steps │ │ terraform │ │
│ │ │ │ │ apply │ │
│ │ ▼ │ │ │ │ │
│ │ Hard to Repeat│ │ ▼ │ │
│ └────────────────┘ │ Reproducible │ │
│ └────────────────┘ │
│ │
│ Benefits: │
│ ✓ Version control for infrastructure │
│ ✓ Automated provisioning │
│ ✓ Consistent and reproducible environments │
│ ✓ Self-documenting infrastructure │
│ ✓ Easier collaboration and review │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Terraform Overview │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌───────────────────────────────────────────────────────────────────┐ │
│ │ Terraform │ │
│ │ │ │
│ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ │
│ │ │ Configuration │ │ Plan │ │ Apply │ │ │
│ │ │ (.tf) │─▶│ Changes │─▶│ Resources │ │ │
│ │ └──────────────┘ └──────────────┘ └──────────────┘ │ │
│ │ │ │
│ │ ┌──────────────────────────────────────────────────────────┐ │ │
│ │ │ Providers │ │ │
│ │ │ AWS │ GCP │ Azure │ Kubernetes │ Docker │ │ │ │
│ │ └──────────────────────────────────────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Terraform vs CloudFormation vs Ansible │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ Feature Terraform CloudFormation Ansible │
│ ────────────────────────────────────────────────────────────────────── │
│ Provider Multi-cloud AWS-only Multi-platform │
│ Language HCL YAML/JSON YAML │
│ State Required Optional Stateless │
│ Approach Declarative Declarative Procedural │
│ Learning Curve Moderate Low Moderate │
│ Mature High High High │
│ Enterprise Terraform Cloud CloudFormation AWX/Tower │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐
│ Terraform Workflow │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ 1. Write │
│ ┌─────────────────┐ │
│ │ main.tf │ │
│ │ variables.tf │ Define infrastructure │
│ │ outputs.tf │ │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ 2. Init │
│ ┌─────────────────┐ │
│ │ terraform init │ Initialize providers │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ 3. Plan │
│ ┌─────────────────┐ │
│ │ terraform plan │ Preview changes │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ 4. Apply │
│ ┌─────────────────┐ │
│ │ terraform apply │ Provision resources │
│ └────────┬────────┘ │
│ │ │
│ ▼ │
│ 5. State │
│ ┌─────────────────┐ │
│ │ terraform.tfstate│ Track managed resources │
│ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘
Terminal window
# Download Terraform
curl -LO https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip
# Unzip
unzip terraform_1.6.6_linux_amd64.zip
# Move to PATH
sudo mv terraform /usr/local/bin/
# Verify
terraform -version
Terminal window
brew install terraform
  1. Download from https://www.terraform.io/downloads
  2. Extract to a folder
  3. Add to PATH
  4. Verify with terraform -version
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
}
}
Terminal window
# Initialize Terraform
terraform init
# Create execution plan
terraform plan
# Apply changes
terraform apply
# Destroy resources
terraform destroy

Providers are plugins that Terraform uses to interact with cloud platforms and services:

# AWS Provider
provider "aws" {
region = "us-east-1"
}
# Azure Provider
provider "azurerm" {
features {}
subscription_id = "..."
}
# GCP Provider
provider "google" {
project = "my-project"
region = "us-central1"
}
# Kubernetes Provider
provider "kubernetes" {
config_path = "~/.kube/config"
}

The Terraform Registry contains thousands of providers and modules:

  • Providers: AWS, Azure, GCP, Kubernetes, etc.
  • Modules: Reusable Terraform configurations
# Using a module from registry
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.0.0"
name = "my-vpc"
cidr = "10.0.0.0/16"
}

In this chapter, you learned:

  • What is Infrastructure as Code
  • What is Terraform and how it works
  • Terraform workflow (Write, Init, Plan, Apply, State)
  • Installing Terraform
  • Basic Terraform example
  • Providers overview