> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Declarative vs Imperative Why Terraform Works the Way It Does

> Describes why Terraform uses a declarative infrastructure model versus imperative scripts, highlighting dependency graph, state management, minimal changes, and plan before apply workflow

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

```hcl theme={null}
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)

```bash theme={null}
# 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.

<Callout icon="lightbulb" color="#1CB2FE">
  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.
</Callout>

## 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

| 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

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

* [Terraform documentation](https://www.terraform.io/docs)
* [Kubernetes: Declarative vs Imperative](https://kubernetes.io/docs/concepts/overview/working-with-objects/object-management/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/be082b2a-db28-4bed-84e4-233393a3aafa/lesson/a4e5f0e2-a9a1-4bf8-a592-0157c396577b" />
</CardGroup>
