Infrastructure as Code & Terraform Introduction
Infrastructure as Code (IaC) replaces manual provisioning with version-controlled configuration files, delivering consistency, repeatability, and automation. Terraform is the dominant multi-cloud IaC tool: declarative, provider-agnostic, and backed by a rich ecosystem of 3,000+ providers. Understanding its architecture (Core, Providers, State, Registry) and the declarative vs procedural distinction is foundational for the Terraform Associate exam.
1. What is Infrastructure as Code?
Infrastructure as Code (IaC) is the practice of managing and provisioning infrastructure through machine-readable configuration files instead of manual processes, interactive UIs, or one-off scripts. Infrastructure is treated with the same discipline as application code — version controlled, reviewed, tested, and deployed through automated pipelines.
2. Why IaC? The Core Benefits
| Benefit | Problem It Solves | How Terraform Delivers It |
|---|---|---|
| Consistency | "Works on my environment" drift between dev/staging/prod | Same config produces identical infrastructure every run |
| Reproducibility | Can't recreate last month's environment | terraform apply from the same commit = same infra |
| Version Control | No audit trail of who changed what | Every change is a Git commit with author, timestamp, diff |
| Idempotency | Running a script twice breaks things | Terraform converges to desired state; running twice is safe |
| Speed | Weeks to provision manually | Full environment in minutes |
| Documentation | Wikis go stale | Config files are always accurate living documentation |
| Disaster Recovery | Rebuilding from scratch takes days | Recreate entire environment with one command |
| Cost Control | Forgotten resources accumulate costs | terraform destroy cleans up everything completely |
3. Declarative vs Procedural IaC
This is one of the most important conceptual distinctions for the exam:
| Aspect | Declarative (Terraform) | Procedural (Ansible, Bash) |
|---|---|---|
| You write | The desired end state | The sequence of steps |
| Idempotency | Built-in — Terraform diffs and only changes what's needed | Must be coded manually with conditionals |
| Re-running | Safe — converges to same state | May fail or create duplicates |
| Complexity | Terraform manages the "how" | You manage the "how" |
| Example | "I want 3 EC2 instances of type t3.micro" | "Create an EC2 instance IF one doesn't exist, ELSE skip" |
4. Terraform Overview
Terraform was created by HashiCorp in 2014. It is:
- Open-source under the Business Source License (BSL 1.1) since August 2023 (was MPL 2.0)
- OpenTofu is the open-source fork maintaining MPL 2.0 licensing
- Written in Go; configuration written in HCL (HashiCorp Configuration Language)
- Provider-agnostic — one tool for AWS, Azure, GCP, Kubernetes, GitHub, Datadog, and 3,000+ others
- Currently the industry-standard IaC tool for multi-cloud infrastructure
5. Terraform Architecture
Terraform Core
- Reads and parses
.tfconfiguration files - Builds a dependency graph of resources (determines creation order)
- Compares desired state (config) against current state (state file)
- Produces an execution plan — shows exactly what will be created, changed, or destroyed
- Communicates with providers via RPC (Remote Procedure Call) over a plugin protocol
Providers
- Standalone Go binaries downloaded during
terraform init - Each provider implements a set of resource types and data sources
- Translate Terraform resource declarations into real API calls
- Versioned independently from Terraform core
- Examples:
hashicorp/aws,hashicorp/azurerm,hashicorp/google,hashicorp/kubernetes
State
- A JSON file (
terraform.tfstate) mapping configuration resources to real-world resource IDs - Enables Terraform to know what already exists vs what to create/update/destroy
- Stores metadata, resource dependencies, and provider information
- Should be stored remotely (S3, Azure Blob, GCS, Terraform Cloud) for team usage
Registry
registry.terraform.io— public hub for providers and reusable modules- Providers: search by cloud provider, filter by tier (official, partner, community)
- Modules: pre-built, composable infrastructure patterns (e.g., "VPC module", "EKS cluster module")
6. Key Terraform Building Blocks
1# Provider — tells Terraform which platform to use
2terraform {
3 required_providers {
4 aws = {
5 source = "hashicorp/aws"
6 version = "~> 5.0"
7 }
8 }
9}
10
11provider "aws" {
12 region = "us-east-1"
13}
14
15# Resource — a single piece of infrastructure to manage
16resource "aws_instance" "web" {
17 ami = "ami-0c55b159cbfafe1f0"
18 instance_type = "t3.micro"
19
20 tags = {
21 Name = "web-server"
22 }
23}
24
25# Data Source — read existing infrastructure (not managed by this config)
26data "aws_vpc" "default" {
27 default = true
28}
29
30# Output — expose values after apply
31output "instance_public_ip" {
32 value = aws_instance.web.public_ip
33}
34
35# Variable — parameterize the configuration
36variable "instance_type" {
37 type = string
38 default = "t3.micro"
39}
40
41# Local — intermediate computed values
42locals {
43 name_prefix = "prod-web"
44}
45
46# Module — reuse a collection of resources
47module "vpc" {
48 source = "terraform-aws-modules/vpc/aws"
49 version = "~> 5.0"
50 name = "my-vpc"
51 cidr = "10.0.0.0/16"
52}| Block Type | Purpose |
|---|---|
terraform {} | Configure Terraform itself (required providers, backend, version constraints) |
provider {} | Configure a provider (credentials, region, endpoints) |
resource {} | Declare an infrastructure object to create and manage |
data {} | Read existing infrastructure not managed by this config |
variable {} | Parameterize configuration; accepts input values |
output {} | Expose values after apply; share values between modules |
locals {} | Define intermediate computed values within a module |
module {} | Instantiate a reusable module (local or from Registry) |
7. Terraform vs Other IaC Tools
| Tool | Approach | Scope | Language | State | Best For |
|---|---|---|---|---|---|
| Terraform | Declarative | Multi-cloud | HCL | Yes | Multi-cloud provisioning |
| AWS CloudFormation | Declarative | AWS only | JSON/YAML | Yes (stacks) | AWS-only shops |
| Ansible | Procedural | Multi-purpose | YAML | No | Config management, patching |
| Pulumi | Declarative | Multi-cloud | Python/TS/Go/C# | Yes | Developers preferring real languages |
| Chef / Puppet | Declarative | Config mgmt | Ruby DSL | No | OS-level configuration |
| CDK for Terraform | Declarative | Multi-cloud | Python/TS/Go | Yes (Terraform state) | Developers wanting Terraform with code |
Key differentiators of Terraform:
- Execution plans — preview changes before applying (no other tool shows this as clearly)
- State management — tracks what it manages so it can detect drift
- Provider ecosystem — 3,000+ providers covering virtually every platform
- Module reusability — share and reuse patterns via the public Registry
8. Terraform Editions
| Edition | Who It's For | Key Features |
|---|---|---|
| Terraform OSS (open-source) | Individuals and small teams | CLI, all providers, local/remote state |
| Terraform Cloud (Free tier) | Small teams | Remote state, remote runs, 500 resources |
| Terraform Cloud (Plus) | Growing teams | Audit logs, SSO, policy enforcement (Sentinel) |
| Terraform Enterprise | Large organizations | Self-hosted, SAML, advanced audit, private registry |
| OpenTofu | OSS purists | MPL 2.0 fork of Terraform, community governed |
9. The Terraform Workflow at a Glance
10. Quick Reference
| Concept | Key Fact |
|---|---|
| IaC definition | Infrastructure managed via version-controlled config files |
| Declarative | Describe desired end state; tool figures out how to get there |
| Procedural | Describe the steps to reach the state (Ansible, Bash) |
| Idempotency | Running Terraform multiple times always converges to the same state |
| HCL | HashiCorp Configuration Language — human-readable, supports expressions |
| Provider | Plugin that translates resource declarations into API calls |
| State file | JSON mapping config resources to real-world infrastructure IDs |
| Registry | registry.terraform.io — hub for providers and reusable modules |
| Terraform Core | Parses config, builds dependency graph, orchestrates apply |
| Terraform Cloud | Managed service for remote state, remote runs, collaboration |
| OpenTofu | MPL 2.0 open-source fork of Terraform maintained by the community |
| terraform init | Downloads providers/modules; must run before plan or apply |
| terraform plan | Dry run — shows what will change without touching real infrastructure |
| terraform apply | Executes the plan and creates/updates/destroys real infrastructure |
Practice Questions4
Q1. What is Infrastructure as Code (IaC)?
Select one answer before revealing.
Q2. Which of the following statements correctly differentiate Terraform from AWS CloudFormation? (Select all that apply — more than one answer may be correct.)
Select one answer before revealing.
Q3. What language is Terraform configuration primarily written in?
Select one answer before revealing.
Q4. What is the purpose of `terraform init`?
Select one answer before revealing.