What is Infrastructure as Code
Managing infrastructure through code
Infrastructure as Code (IaC) is an approach to managing and deploying infrastructure using machine-readable configuration files instead of manual configuration.
Benefits
- Version control — infrastructure in Git, change history
- Reproducibility — same environment everywhere
- Automation — fast deployment without manual actions
- Documentation — code serves as documentation
IaC Tools
- Terraform — cloud-agnostic, declarative
- AWS CloudFormation — for AWS
- Pulumi — IaC in regular languages (Python, TypeScript)
- Ansible — configuration management
Declarative vs Imperative
- Declarative (Terraform): describe desired state
- Imperative (scripts): describe steps to achieve
Practices
- Storing state in remote storage
- Modules for code reuse
- CI/CD for automatic application
- Policies and checks (OPA, Sentinel)
Terraform Example
resource "aws_instance" "web" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}