Declarative vs Imperative — the core idea
- Declarative: describe the desired final state of your infrastructure (the “what”). Terraform figures out the sequence of actions required to reach that state.
- Imperative: provide explicit step-by-step commands that perform changes in a specific order (the “how”).
High-level examples
Declarative (what you express)- You declare: “I want an EC2 instance with these properties.”
- Terraform computes: which API calls to make, in what order, and what must change to reach that state from the current state.
- You write a script with ordered steps: create VM, SSH to configure it, attach it to the network, run commands.
- The script author must manage ordering, error handling, retries, idempotency, and safe re-runs.
Concrete examples
Terraform (declarative HCL)- This configuration describes the desired end state: an EC2 instance with the specified AMI, instance type, and tag.
terraform plancompares the desired configuration with the saved state and the live infrastructure (after refresh), showing a preview of changes.terraform applyexecutes the API calls needed to create or modify resources to match the configuration.
- This script explicitly enumerates actions and their order.
- The author must make the workflow idempotent, handle ordering and transient errors, and ensure the script is safe to re-run.
Why Terraform’s declarative model helps
- Plan and Preview:
terraform plancomputes a change plan so you can review intended modifications before applying them. - Resource Graph: Terraform builds a dependency graph from your configuration and can parallelize independent operations while respecting dependencies.
- Idempotency: Re-applying the same configuration should converge to the same target state; idempotency depends on provider behavior and external changes.
- Drift detection: Terraform refreshes state and compares configuration and saved state to real-world resources to detect drift.
- Fewer accidental mistakes: You express the desired end state rather than scripting every step; this reduces manual ordering mistakes and hidden side effects.
Comparison: Declarative vs Imperative
What “declarative” does not mean
Declarative does not mean Terraform performs no imperative operations. Terraform still issues API calls (imperative operations) under the hood to reach the declared state. It can also runprovisioner or remote-exec actions when configured, but Terraform determines the execution order and coordinates changes to match your declared configuration.
State and caveats
Terraform relies on state to map configuration to real-world resources. Protect and manage your state (e.g., remote state backends, state locking, and access control) to avoid conflicts and accidental resource replacements.
- Remote state backends (e.g., S3, Terraform Cloud) enable locking, collaboration, and safer workflows.
- State is the source of truth for mapping resource IDs and attributes — manual edits or corruption can cause unexpected behavior.
- External changes (out-of-band modifications) require a refresh and may produce drift that Terraform will detect during
plan.
Practical tips
- Always run
terraform planbeforeterraform applyto review changes. - Use remote state with locking in team environments to prevent race conditions.
- Avoid imperative provisioners for configuration management when possible; use configuration management tools (Ansible, Chef) or immutable images instead.
- Break large configurations into modules and smaller workspaces to reduce blast radius and improve readability.
Summary
- Declarative configuration expresses the desired end state.
- Terraform handles sequencing, dependency resolution, and execution to achieve that state.
- This approach improves maintainability, reduces manual errors, and enables reproducible, auditable infrastructure changes.