Skip to content

Architecture

Terraform’s architecture is designed to abstract infrastructure management by decoupling configuration from execution, enabling declarative, version-controlled infrastructure provisioning. At its core, Terraform uses a combination of providers, resources, configurations, and a plan/apply workflow to manage infrastructure as code (IaC). This architecture ensures consistency, scalability, and auditability across environments.


Core Concepts

Providers

Providers are plugins that enable Terraform to interact with infrastructure platforms like AWS, Azure, GCP, or on-premises systems. Each provider defines how Terraform creates, reads, updates, and deletes resources.
Example:

provider "aws" {
  region = "us-west-2"
}
Providers are initialized via terraform init, which downloads and configures them.


Resources

Resources represent infrastructure components (e.g., virtual machines, storage buckets) defined in your configuration. Each resource has a type (e.g., aws_instance) and arguments (e.g., instance type, AMI).
Example:

resource "aws_instance" "example" {
  ami           = "ami-0c55b1d6b7d0768c5"
  instance_type = "t2.micro"
}
Resources are managed through CRUD operations, with Terraform tracking their state to ensure consistency.


Configurations

Configurations are written in HashiCorp Configuration Language (HCL) and define the desired infrastructure state. They include providers, resources, and variables. Configurations are version-controlled and reusable.
Example:

variable "instance_type" {
  default = "t2.micro"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b1d6b7d0768c5"
  instance_type = var.instance_type
}
Variables allow parameterization, making configurations flexible for different environments.


Plan/Apply Workflow

Terraform’s workflow consists of two phases:
1. Plan: Analyzes the configuration and generates an execution plan, showing changes without applying them.

terraform plan
2. Apply: Executes the plan, creating or modifying infrastructure.
terraform apply
The state file (managed by Terraform) tracks resource IDs and attributes, ensuring idempotent operations.


State Management

Terraform uses a state file to map resources to their actual IDs in the infrastructure. This state is critical for operations like destruction (terraform destroy) and dependency resolution. The state can be stored locally or in remote backends (e.g., S3, Consul) for team collaboration.


Key takeaways

  • Providers enable Terra, to interact with cloud platforms and APIs.
  • Resources define the infrastructure components to provision.
  • Configurations are declarative, version-controlled files that describe the desired state.
  • The plan/apply workflow ensures predictable, auditable infrastructure changes.
  • The state file is essential for tracking resources and ensuring consistency across operations.