Declarative vs Imperative — the core idea
- Declarative: You describe the desired end state of your infrastructure. Terraform figures out what actions are necessary to reach that state.
- Imperative: You list the exact steps to perform (create VM, run commands, attach network, etc.). You control the workflow.
Example: Declarative Terraform configuration
terraform plan— show the proposed set of actions without making changesterraform apply— execute the planned actions and update state
Equivalent Imperative steps (conceptual)
Because you describe the desired final state rather than the steps to get there, Terraform can compute an efficient change plan, manage dependencies automatically, and provide a predictable
plan/apply workflow.Benefits of Terraform’s declarative model
- Focus on intent: declare what you want; avoid scripting how to do it.
- Dependency graph: Terraform understands inter-resource relationships and orders operations correctly.
- Minimal changes: Terraform computes the smallest set of actions to reconcile current and desired state.
- Safe workflow:
terraform planlets you review proposed changes before applying them. - State management: Terraform stores state to map configuration to real resources; remote state and locking enable collaboration.
Declarative vs Imperative — quick comparison
| Aspect | Declarative (Terraform) | Imperative (CLI / scripts) |
|---|---|---|
| Primary intent | Describe final state | Describe steps/commands |
| Change computation | Engine computes diff and plan | You control changes and order |
| Idempotency | Built-in via plan/apply and state | Must be implemented in scripts |
| Dependency handling | Automatic dependency graph | Manually managed by scripting |
| Review before apply | terraform plan | Depends on tooling / manual checks |
Typical Terraform workflow
- Write configuration files (
.tf) that declare resources and inputs. - Initialize the working directory:
terraform init. - Preview planned actions:
terraform plan. - Apply changes:
terraform apply. - Manage state: use remote state backends (e.g., S3, Terraform Cloud) and enable locking for team workflows.
When to choose declarative vs imperative
- Choose declarative (Terraform) when you want reproducible, version-controlled infrastructure and automatic dependency resolution across multiple resources and providers.
- Choose imperative scripts when performing one-off ad-hoc operations or when you need very fine-grained procedural control that cannot be easily expressed in a declarative model.