
Why SREs adopt IaC
- Eliminate snowflake servers — provisioned resources are consistent and reproducible.
- Predictable disaster recovery — rebuild environments from code.
- Auditability — Git history provides a trace of who changed what and when.
- Safer deployments — preview and test infrastructure changes before applying them, reducing incidents and on-call interruptions.

Popular IaC tools
Use the right tool for the job: provisioning, configuration management, or policy enforcement. Below is a quick reference.
IaC best practices (practical checklist)
- Version control everything. If it isn’t in Git, it effectively doesn’t exist — avoid manual cloud-console changes.
- Use variables and parameterization instead of hard-coded values to support multiple environments.
- Create reusable modules and follow DRY (Don’t Repeat Yourself) patterns.
- Treat infrastructure changes like application code: require pull requests, peer review, and automated checks (lint, security scans, tests).
- Isolate secrets and store them in secure vaults or CI/CD secrets (never in source).

Testing infrastructure changes
Infrastructure must be validated like application code. Integrate local checks and CI pipeline scans. Common local commands:
Example local usage:
Policy as code
Encode guardrails so issues are caught before any apply step runs. Policy-as-code tools like Open Policy Agent (OPA) and HashiCorp Sentinel can prevent unsafe changes (for example: deny publicly-readable S3 buckets, require backup tags on critical resources, or block production-affecting changes without explicit approvals).Real-world example: automating AWS IAM user creation
Manual IAM user creation at scale is error-prone: missing MFA, inconsistent policy attachments, or different tags across accounts. IaC lets you define users in variables, iterate to create them, attach managed or inline policies, and enforce consistent metadata (tags, MFA enforcement via policy, etc.).
- Structured variable for users:
- Create users dynamically and tag them:
- Attach a managed policy example:
Repository walkthrough (summary)
The sample repo (KodeKloud Records Terraform Infrastructure) demonstrates these concepts: remote state in S3, modular IAM user creation, and CI/CD workflows for plan/apply/destroy. Clone or fork the repo to follow along. Top-level Terraform configuration (remote state and S3 backend excerpt):GitHub Actions to apply Terraform
Store AWS credentials as repository secrets and give the CI user least privilege required for the workflow. Use a workflow that checks out code, sets up Terraform, runs init, and applies. Example apply workflow (trigger on specific branches):Store credentials in GitHub repository secrets and never commit them to the repo. Grant the CI user least privilege needed for the workflow.



Infrastructure drift: detection and remediation
A common challenge is infrastructure drift: live infrastructure that no longer matches code. Example: someone urgently grants CloudWatch access in the AWS console instead of updating Terraform — the configuration diverges.
- Run
terraform planagainst the live environment. Terraform compares real resources to the state file and highlights differences. - Integrate periodic drift detection (scheduled CI jobs) to detect drift proactively.
- Accept the manual change by updating your IaC (recommended when the manual change is valid).
Example: Add CloudWatch permissions in Terraform:
- Reject the manual change and restore the declared state:
- Import the manually-created resource into Terraform so it becomes part of the managed state:
Closing notes
This lesson covered IaC fundamentals for SREs: why IaC matters, tooling, best practices, testing and CI/CD, policy as code, an IAM automation example, and drift detection/remediation. Configuration management (Ansible, Chef, Puppet) is a complementary topic focused on ensuring consistent software and runtime configuration across fleets and should be studied alongside IaC for full-stack reliability.Links and references
- Terraform documentation: https://www.terraform.io/docs
- tfsec: https://github.com/aquasecurity/tfsec
- tflint: https://github.com/terraform-linters/tflint
- GitHub Actions docs: https://docs.github.com/actions
- Open Policy Agent (OPA): https://www.openpolicyagent.org/