πΉ Terraform Architecture β A Senior DevOps Engineer Explanation
Terraform follows a Client-Server (Agentless) architecture, where the Terraform CLI (Client) interacts directly with Infrastructure Providers (APIs) to create, update, or destroy resources. It does not require an agent on the managed infrastructure, making it lightweight and efficient.
1.2 Terraform Project Structure
A typical Terraform project is organized with specific files and directories:
.terraform/: Hidden directory for keeping provider plugins and modules.
providers/: Stores the downloaded provider plugins.
terraform.tfstate: Stores the current state of the managed infrastructure.
terraform.tfstate.backup: Backup of the previous Terraform state.
variables.tf: Defines input variables used in Terraform configurations.
variables.tfvars: Provides values for input variables defined in variables.tf.
1οΈβ£ Core Components of Terraform Architecture
Terraform consists of 5 major components:
1.1 Configuration Files (.tf Files)
Written in HCL (HashiCorp Configuration Language).
Define resources like servers, networks, and databases.
Example:
1.2 Terraform Core (CLI / Engine)
The heart of Terraform, responsible for:
β Parsing .tf configuration files.
β Managing Terraform state (terraform.tfstate).
β Communicating with Providers (AWS, Azure, GCP, Kubernetes, etc.).
β Planning and applying infrastructure changes.
π Terraform CLI Commands
Command
Purpose
terraform init
Initializes Terraform, downloads providers.
terraform plan
Shows what Terraform will change before applying.
terraform apply
Creates or updates infrastructure.
terraform destroy
Deletes infrastructure.
1.3 Providers (AWS, Azure, GCP, Kubernetes, etc.)
Providers interact with cloud APIs to provision infrastructure.
Terraform downloads providers during terraform init.
Example: AWS provider configuration:
π Common Terraform Providers
Provider
Purpose
aws
Manages AWS resources (EC2, S3, RDS, etc.)
azure
Manages Azure resources (VMs, Storage, etc.)
google
Manages GCP resources (GCE, GKE, etc.)
kubernetes
Manages Kubernetes resources (Pods, Deployments)
1.4 State File (terraform.tfstate)
Terraform stores the current state of the infrastructure in terraform.tfstate.
It helps Terraform track what exists and what needs to change.
The state file can be stored locally or remotely (S3, Terraform Cloud, etc.).
π‘ Example State File (Simplified JSON)
1.5 Backends (Local / Remote State Storage)
Stores the Terraform state file (terraform.tfstate) securely.
Local Backend: Stores state in the local filesystem (default).
Remote Backend (Best Practice): Stores state in cloud storage to enable team collaboration.
π Example: Storing State in AWS S3
2οΈβ£ Terraform Workflow (End-to-End Flow)
Step 1οΈβ£: Write Configuration (.tf Files)
Define infrastructure using Terraform.
Example: Creating an AWS EC2 instance:
Step 2οΈβ£: Initialize (terraform init)
Downloads the required Terraform providers.
Initializes the Terraform project.
Step 3οΈβ£: Plan (terraform plan)
Compares the desired state (configuration files) with the current state (terraform.tfstate).
Outputs a preview of changes.
Step 4οΈβ£: Apply (terraform apply)
Executes the plan and creates/modifies infrastructure.
Step 5οΈβ£: Store & Manage State (terraform.tfstate)
Terraform updates the state file to track the infrastructure.
Step 6οΈβ£: Destroy (terraform destroy)
Deletes all infrastructure resources.
3οΈβ£ Advanced Terraform Concepts
πΉ Modules
Reusable Terraform configurations for better code modularity.
Example:
πΉ Remote State Management (Terraform Cloud)
State locking & team collaboration.
Example:
πΉ Terraform Workspaces
Used for managing multiple environments (dev, staging, prod).
Example:
4οΈβ£ Terraform Architecture Diagram
5οΈβ£ Terraform vs Ansible Architecture
Feature
Terraform ποΈ
Ansible βοΈ
Type
Declarative
Imperative
State Mgmt
Yes (.tfstate) β
No β
Agent?
No (Agentless) β
No (SSH-based) β
Provisioning
Infra Provisioning
Configuration Management
Idempotent?
Yes β
Yes β
Multi-Cloud?
Yes β
Limited β
πΉ Summary β Why Terraform?
β Declarative Approach β Defines desired state, and Terraform ensures it happens.
β Agentless Architecture β Communicates directly with cloud providers via API.
β State Management β Tracks infrastructure changes using terraform.tfstate.
β Multi-Cloud Support β Works across AWS, Azure, GCP, Kubernetes.
β Immutable Infrastructure β Prevents configuration drift.
π Final Thoughts
Terraform provides scalability, automation, and consistency in managing infrastructure. By following best practices like using modules, remote state, and workspaces, teams can efficiently manage infrastructure across different environments.
π‘ Next Steps:
Want hands-on Terraform examples? Let me know!
Need interview scenarios? I can provide Terraform-specific problem-solving questions! π