Architecture

πŸ”Ή 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.

      • registry.terraform.io/: Contains provider registry data.

        • hashicorp/aws/: AWS provider directory.

          • terraform-provider-aws_vX.X.X_xX: AWS provider binary.

  • .terraform.lock.hcl: Locks provider versions to ensure consistency.

  • main.tf: Main configuration file defining resources.

  • output.tf: Defines output variables for the Terraform run.

  • provider.tf: Specifies provider configurations (e.g., AWS, Azure, GCP).

  • 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! πŸš€

Last updated