Infrastructure as Code (IaC)
Managing and provisioning computing infrastructure through machine-readable configuration files rather than manual processes or interactive tools.
Detailed Explanation
IaC lets you define servers, networks, databases, and other infrastructure in code files that are version-controlled, reviewed, and deployed like application code. Instead of clicking through a web console to create a server, you write a configuration file and apply it.
IaC tools: Terraform (most popular, multi-cloud, HCL language), Pulumi (uses real programming languages), AWS CloudFormation (AWS-specific), and Ansible (configuration management). IaC provides: reproducible infrastructure, version-controlled changes, automated provisioning, and documentation as code.
Why It Matters
IaC eliminates manual infrastructure configuration, enables reproducible environments, and makes infrastructure changes auditable and reversible.
Real-World Example
A team defines their entire infrastructure in Terraform: VPC, subnets, ECS cluster, RDS database, and CloudFront distribution. Running `terraform apply` provisions everything. Running `terraform destroy` tears it all down.
When to Use
For any production infrastructure. Even small projects benefit from IaC for reproducible staging environments and disaster recovery.
Advantages
- Reproducible infrastructure
- Version-controlled changes
- Automated provisioning
- Documentation as code
- Disaster recovery through re-provisioning
Disadvantages
- Learning curve for IaC tools
- State management complexity
- Drift between code and actual infrastructure
- Debugging failed provisions
- Provider-specific configurations
Related Terms
Frequently Asked Questions
What is the difference between Terraform and Ansible?
Terraform provisions infrastructure (creates servers, networks). Ansible configures existing infrastructure (installs software, manages configuration). They are complementary—Terraform creates, Ansible configures.
What is Terraform state?
Terraform state is a file that maps your configuration to real-world resources. It tracks what Terraform manages and enables planning changes. Store state remotely (S3, Terraform Cloud) for team collaboration.
How do I handle secrets in IaC?
Use your provider's secret management: AWS Secrets Manager, Vault, or environment variables. Never hardcode secrets in IaC files. Use sensitive variables in Terraform to mask secrets in output.
Should I use Terraform or Pulumi?
Terraform uses HCL (declarative), has a larger ecosystem, and is more widely adopted. Pulumi uses real programming languages (TypeScript, Python), which is more familiar for developers. Choose based on your team's preferences.
What is infrastructure drift?
Drift occurs when the actual infrastructure differs from what is defined in code (manual changes, emergency fixes). Use `terraform plan` to detect drift and `terraform refresh` to update state. Avoid manual changes to IaC-managed infrastructure.