Terraform_introduction
Chapter 33: Terraform Introduction
Section titled “Chapter 33: 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.
What is Infrastructure as Code?
Section titled “What is Infrastructure as Code?”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 ││ │└─────────────────────────────────────────────────────────────────────────────┘What is Terraform?
Section titled “What is Terraform?”┌─────────────────────────────────────────────────────────────────────────────┐│ Terraform Overview │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌───────────────────────────────────────────────────────────────────┐ ││ │ Terraform │ ││ │ │ ││ │ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │ ││ │ │ Configuration │ │ Plan │ │ Apply │ │ ││ │ │ (.tf) │─▶│ Changes │─▶│ Resources │ │ ││ │ └──────────────┘ └──────────────┘ └──────────────┘ │ ││ │ │ ││ │ ┌──────────────────────────────────────────────────────────┐ │ ││ │ │ Providers │ │ ││ │ │ AWS │ GCP │ Azure │ Kubernetes │ Docker │ │ │ ││ │ └──────────────────────────────────────────────────────────┘ │ ││ │ │ ││ └───────────────────────────────────────────────────────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Terraform vs Other IaC Tools
Section titled “Terraform vs Other IaC Tools”┌─────────────────────────────────────────────────────────────────────────────┐│ 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
Section titled “Terraform Workflow”┌─────────────────────────────────────────────────────────────────────────────┐│ 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 ││ └─────────────────┘ ││ │└─────────────────────────────────────────────────────────────────────────────┘Installing Terraform
Section titled “Installing Terraform”Linux/macOS
Section titled “Linux/macOS”# Download Terraformcurl -LO https://releases.hashicorp.com/terraform/1.6.6/terraform_1.6.6_linux_amd64.zip
# Unzipunzip terraform_1.6.6_linux_amd64.zip
# Move to PATHsudo mv terraform /usr/local/bin/
# Verifyterraform -versionmacOS with Homebrew
Section titled “macOS with Homebrew”brew install terraformWindows
Section titled “Windows”- Download from https://www.terraform.io/downloads
- Extract to a folder
- Add to PATH
- Verify with
terraform -version
Basic Example
Section titled “Basic Example”main.tf
Section titled “main.tf”provider "aws" { region = "us-east-1"}
resource "aws_instance" "web_server" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro"
tags = { Name = "WebServer" }}Running Terraform
Section titled “Running Terraform”# Initialize Terraformterraform init
# Create execution planterraform plan
# Apply changesterraform apply
# Destroy resourcesterraform destroyProviders
Section titled “Providers”Providers are plugins that Terraform uses to interact with cloud platforms and services:
# AWS Providerprovider "aws" { region = "us-east-1"}
# Azure Providerprovider "azurerm" { features {} subscription_id = "..."}
# GCP Providerprovider "google" { project = "my-project" region = "us-central1"}
# Kubernetes Providerprovider "kubernetes" { config_path = "~/.kube/config"}Terraform Registry
Section titled “Terraform Registry”The Terraform Registry contains thousands of providers and modules:
- Providers: AWS, Azure, GCP, Kubernetes, etc.
- Modules: Reusable Terraform configurations
# Using a module from registrymodule "vpc" { source = "terraform-aws-modules/vpc/aws" version = "3.0.0"
name = "my-vpc" cidr = "10.0.0.0/16"}Summary
Section titled “Summary”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