Terraform & IaC Foundations
Master enterprise Infrastructure as Code (IaC) with HashiCorp Terraform: declarative workflows, state locking, module architecture, and immutable cloud delivery.
Terraform & IaC Foundations
Section titled βTerraform & IaC FoundationsβInfrastructure as Code (IaC) treats infrastructure configurations as software code. Instead of clicking through cloud web consoles (ClickOps), your fleet is defined declaratively, version-controlled, automated, and auditable.
+--------------------+ +---------------------+ +--------------------+| Declarative HCL | ---> | Terraform Engine | ---> | Cloud Provider || (main.tf, var.tf) | | (Plan, State, Graph)| | (AWS, GCP, Azure)|+--------------------+ +---------------------+ +--------------------+1. Why Declarative IaC Matters
Section titled β1. Why Declarative IaC MattersβTraditional scripting (Imperative) tells the system how to do something step-by-step (e.g. aws ec2 run-instances ...). Declarative IaC (Terraform) tells the engine what state the target infrastructure must achieve.
| Paradigm | Philosophy | Tools | Drift Management |
|---|---|---|---|
| Imperative | Step-by-step instructions (βRun this scriptβ) | Bash, AWS CLI, Python Boto3 | Manual detection; high risk of drift |
| Declarative | Desired target state (βEnsure this existsβ) | Terraform, OpenTofu, Pulumi | Built-in reconciliation via state files |
2. The Core Terraform Workflow
Section titled β2. The Core Terraform WorkflowβThe modern cloud delivery lifecycle relies on four sequential phases:
[Write HCL Code] ---> terraform init ---> terraform plan ---> terraform applyterraform init: Downloads required cloud provider plugins (e.g., AWS, Kubernetes) and sets up the remote backend.terraform plan: Computes an execution plan by comparing declared code against live state and the target cloud API.terraform apply: Executes the exact graph of changes (Create, In-place Update, Destroy & Recreate).terraform destroy: Safely cleans up and tears down managed resources in reverse dependency order.
3. Remote State & Distributed State Locking
Section titled β3. Remote State & Distributed State LockingβIn production enterprise environments, the Terraform state file (terraform.tfstate) must never be stored in Git repositories (to prevent secret leaks and concurrency collisions).
Production Remote Backend Architecture (AWS S3 + DynamoDB)
Section titled βProduction Remote Backend Architecture (AWS S3 + DynamoDB)βterraform { required_version = ">= 1.5.0" required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }
backend "s3" { bucket = "devsecops-terraform-state-prod" key = "core/networking/terraform.tfstate" region = "us-east-1" encrypt = true dynamodb_table = "terraform-locks" }}- S3 Bucket Encryption: Ensures state files containing sensitive data (passwords, TLS certs) are encrypted at rest with AWS KMS.
- DynamoDB State Locking: Prevents concurrent pipeline runs or teammates from corrupting state during
terraform apply.
4. Module Architecture: Reusability at Scale
Section titled β4. Module Architecture: Reusability at ScaleβEnterprise architectures structure code into Root Modules (environments) and Child Modules (reusable components):
infrastructure/βββ modules/β βββ vpc/β β βββ main.tfβ β βββ variables.tfβ β βββ outputs.tfβ βββ eks-cluster/βββ environments/ βββ dev/ β βββ main.tf βββ prod/ βββ main.tf5. Security & Static Code Analysis for IaC
Section titled β5. Security & Static Code Analysis for IaCβNever push raw Terraform code to production without automated security gates in CI/CD:
tflint: Catches provider-specific deprecations and syntax bugs before running plan.checkov/tfsec: Scans HCL code against CIS benchmarks and identifies insecure defaults (e.g. unencrypted S3 buckets, open 0.0.0.0/0 security groups).infracost: Estimates cloud spend and cost variations directly on GitHub pull requests.