# Infrastructure as Code Principles

> Why infrastructure belongs in code, the difference between declarative and imperative tools, and the provisioning vs configuration split. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/devops/iac-concepts
Last updated: 2026-11-28

Why infrastructure belongs in code, the difference between declarative and imperative tools, and the provisioning vs configuration split.

Beginner · about 3 hours · difficulty 2/5

Learn first: [Git Fundamentals](https://www.internhack.in/roadmaps/tracks/devops/git-fundamentals), [Cloud Computing Concepts](https://www.internhack.in/roadmaps/tracks/devops/cloud-concepts)

Clicking in a console does not scale: nobody can review it, repeat it or roll it back. **Infrastructure as Code (IaC)** stores the desired state of your infrastructure in files, reviews changes through pull requests, and applies them with a tool.

### Declarative vs imperative

- **Declarative** tools (Terraform, CloudFormation, Kubernetes manifests) describe the end state; the tool works out the steps.
- **Imperative** scripts describe steps; rerunning them may break things unless you write the checks yourself.

Declarative tools are **idempotent** by design: applying the same code twice changes nothing the second time.

### Two layers of work

- **Provisioning** creates resources: networks, VMs, databases, DNS records, IAM roles. Terraform, Pulumi, CloudFormation, Bicep.
- **Configuration management** configures what runs inside machines: packages, files, users, services. Ansible, Chef, Puppet, Salt.

With containers and immutable images, configuration management shrinks: you bake an image once (Packer, Dockerfile) and replace machines instead of patching them.

### Practices that matter

- **Drift**: changes made outside the code. Detect it and either import or revert it.
- **Environments from the same code** with different inputs, not copy-pasted folders.
- **Plan before apply**, and have a second person review the plan.
- **Small blast radius**: split state so one mistake cannot delete everything.
- **Immutable infrastructure**: replace, do not mutate.

**Good enough to move on:** you can explain idempotency, drift and immutable infrastructure, and say which tool category you would use to create a VPC and which to install a package on a VM.

## Key concepts

- **Declarative vs imperative**
- **Idempotency**
- **Provisioning vs configuration management**
- **Drift detection**
- **Immutable infrastructure and golden images**
- **Packer for machine images**

## Mini project

Take something you built by hand in the cloud console, write down every setting you chose, and list which parts would become provisioning code and which configuration code.

## Check yourself

Someone changed a security group rule in the console after it was created by code. What is this called, and what are your two options for fixing it?

## Free resources

- [What is Infrastructure as Code with Terraform?](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/infrastructure-as-code) (HashiCorp)
- [Packer documentation](https://developer.hashicorp.com/packer/docs) (HashiCorp)

Previous: [Serverless and Managed Compute](https://www.internhack.in/roadmaps/tracks/devops/serverless) · Next: [Terraform and OpenTofu](https://www.internhack.in/roadmaps/tracks/devops/terraform)

## More in Infrastructure as Code

- [Infrastructure as Code Principles](https://www.internhack.in/roadmaps/tracks/devops/iac-concepts)
- [Terraform and OpenTofu](https://www.internhack.in/roadmaps/tracks/devops/terraform)
- [Pulumi](https://www.internhack.in/roadmaps/tracks/devops/pulumi)
- [CloudFormation, CDK and Bicep](https://www.internhack.in/roadmaps/tracks/devops/cloudformation-cdk)
- [Configuration Management with Ansible](https://www.internhack.in/roadmaps/tracks/devops/ansible)
- [Chef, Puppet and Salt](https://www.internhack.in/roadmaps/tracks/devops/chef-puppet)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [DevOps Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/devops)
