/Infrastructure as Code & Terraform Introduction
Concept
Easy

Infrastructure as Code & Terraform Introduction

8 min read·iacterraformhcldeclarativeprovidersstateregistryidempotencyterraform-associate

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.

Rendering diagram…

2. Why IaC? The Core Benefits

BenefitProblem It SolvesHow Terraform Delivers It
Consistency"Works on my environment" drift between dev/staging/prodSame config produces identical infrastructure every run
ReproducibilityCan't recreate last month's environmentterraform apply from the same commit = same infra
Version ControlNo audit trail of who changed whatEvery change is a Git commit with author, timestamp, diff
IdempotencyRunning a script twice breaks thingsTerraform converges to desired state; running twice is safe
SpeedWeeks to provision manuallyFull environment in minutes
DocumentationWikis go staleConfig files are always accurate living documentation
Disaster RecoveryRebuilding from scratch takes daysRecreate entire environment with one command
Cost ControlForgotten resources accumulate coststerraform destroy cleans up everything completely

3. Declarative vs Procedural IaC

This is one of the most important conceptual distinctions for the exam:

Rendering diagram…
AspectDeclarative (Terraform)Procedural (Ansible, Bash)
You writeThe desired end stateThe sequence of steps
IdempotencyBuilt-in — Terraform diffs and only changes what's neededMust be coded manually with conditionals
Re-runningSafe — converges to same stateMay fail or create duplicates
ComplexityTerraform 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

Rendering diagram…

Terraform Core

  • Reads and parses .tf configuration 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

hcl
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 TypePurpose
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

ToolApproachScopeLanguageStateBest For
TerraformDeclarativeMulti-cloudHCLYesMulti-cloud provisioning
AWS CloudFormationDeclarativeAWS onlyJSON/YAMLYes (stacks)AWS-only shops
AnsibleProceduralMulti-purposeYAMLNoConfig management, patching
PulumiDeclarativeMulti-cloudPython/TS/Go/C#YesDevelopers preferring real languages
Chef / PuppetDeclarativeConfig mgmtRuby DSLNoOS-level configuration
CDK for TerraformDeclarativeMulti-cloudPython/TS/GoYes (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

EditionWho It's ForKey Features
Terraform OSS (open-source)Individuals and small teamsCLI, all providers, local/remote state
Terraform Cloud (Free tier)Small teamsRemote state, remote runs, 500 resources
Terraform Cloud (Plus)Growing teamsAudit logs, SSO, policy enforcement (Sentinel)
Terraform EnterpriseLarge organizationsSelf-hosted, SAML, advanced audit, private registry
OpenTofuOSS puristsMPL 2.0 fork of Terraform, community governed

9. The Terraform Workflow at a Glance

Rendering diagram…

10. Quick Reference

ConceptKey Fact
IaC definitionInfrastructure managed via version-controlled config files
DeclarativeDescribe desired end state; tool figures out how to get there
ProceduralDescribe the steps to reach the state (Ansible, Bash)
IdempotencyRunning Terraform multiple times always converges to the same state
HCLHashiCorp Configuration Language — human-readable, supports expressions
ProviderPlugin that translates resource declarations into API calls
State fileJSON mapping config resources to real-world infrastructure IDs
Registryregistry.terraform.io — hub for providers and reusable modules
Terraform CoreParses config, builds dependency graph, orchestrates apply
Terraform CloudManaged service for remote state, remote runs, collaboration
OpenTofuMPL 2.0 open-source fork of Terraform maintained by the community
terraform initDownloads providers/modules; must run before plan or apply
terraform planDry run — shows what will change without touching real infrastructure
terraform applyExecutes the plan and creates/updates/destroys real infrastructure

Practice Questions4

easy

Q1. What is Infrastructure as Code (IaC)?


Select one answer before revealing.

easy

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.

easy

Q3. What language is Terraform configuration primarily written in?


Select one answer before revealing.

easy

Q4. What is the purpose of `terraform init`?


Select one answer before revealing.