Skip to main content
Terraform follows a declarative model. But what does “declarative” actually mean in infrastructure-as-code, and why does Terraform prefer this approach?

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.
With Terraform, you write configuration files that declare resources and their properties. Terraform then builds a dependency graph, computes a plan of changes to reconcile the real world with your declared state, and applies only the minimal, necessary operations.

Example: Declarative Terraform configuration

resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t2.micro"
  tags = {
    Name = "web-server"
  }
}
Run these commands to preview and apply changes:
  • terraform plan — show the proposed set of actions without making changes
  • terraform apply — execute the planned actions and update state

Equivalent Imperative steps (conceptual)

# Imperative steps (conceptual)
aws ec2 run-instances --image-id ami-0123456789abcdef0 --count 1 --instance-type t2.micro
# SSH into the instance and run provisioning commands
# Manually attach networking/security settings if needed
In the imperative workflow, you must manage order, retries, and idempotency yourself.
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 plan lets 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

AspectDeclarative (Terraform)Imperative (CLI / scripts)
Primary intentDescribe final stateDescribe steps/commands
Change computationEngine computes diff and planYou control changes and order
IdempotencyBuilt-in via plan/apply and stateMust be implemented in scripts
Dependency handlingAutomatic dependency graphManually managed by scripting
Review before applyterraform planDepends on tooling / manual checks

Typical Terraform workflow

  1. Write configuration files (.tf) that declare resources and inputs.
  2. Initialize the working directory: terraform init.
  3. Preview planned actions: terraform plan.
  4. Apply changes: terraform apply.
  5. 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.

References and further reading

Watch Video