Skip to content
About Me
----

Terraform & IaC Foundations

Architect Key Takeaways

Master enterprise Infrastructure as Code (IaC) with HashiCorp Terraform: declarative workflows, state locking, module architecture, and immutable cloud delivery.

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)|
+--------------------+ +---------------------+ +--------------------+

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

The modern cloud delivery lifecycle relies on four sequential phases:

[Write HCL Code] ---> terraform init ---> terraform plan ---> terraform apply
  1. terraform init: Downloads required cloud provider plugins (e.g., AWS, Kubernetes) and sets up the remote backend.
  2. terraform plan: Computes an execution plan by comparing declared code against live state and the target cloud API.
  3. terraform apply: Executes the exact graph of changes (Create, In-place Update, Destroy & Recreate).
  4. terraform destroy: Safely cleans up and tears down managed resources in reverse dependency order.

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.

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.tf

Never push raw Terraform code to production without automated security gates in CI/CD:

  1. tflint: Catches provider-specific deprecations and syntax bugs before running plan.
  2. 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).
  3. infracost: Estimates cloud spend and cost variations directly on GitHub pull requests.