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

# Terraform vs Crossplane

> Compares Terraform and Crossplane using a concrete AWS PostgreSQL provisioning example, highlighting run-on-demand IaC versus Kubernetes-native continuous reconciliation and drift remediation.

How is Crossplane different from Terraform? This page removes ambiguity with a single concrete scenario: a developer needs a PostgreSQL database on AWS.

![A dark slide showing simple icons and labels: a user icon labeled "Developer" and a PostgreSQL elephant labeled "Postgres" at the top, two small tool/service icons on the right, and a large outlined box at the bottom marked with an AWS logo and the word "AWS."](https://mintcdn.com/kodekloud-c4ac6d9a/IK4ISE2GFis3psR1/images/DevOps-Interview-Questions-and-Answers-Scenario-Based-Prep/CICD-DevOps/Terraform-vs-Crossplane/developer-postgres-aws-diagram.jpg?fit=max\&auto=format\&n=IK4ISE2GFis3psR1\&q=85\&s=343b81b4b72317f449c76e190d60ab32)

## High-level difference (short)

* Terraform: declarative Infrastructure as Code (IaC) that runs on demand — changes occur when you execute Terraform commands.
* Crossplane: Kubernetes-native controllers that continuously reconcile external cloud resources to match Kubernetes manifests.

## Example scenario: provision PostgreSQL on AWS

A developer wants a managed PostgreSQL instance on AWS. Below are two idiomatic ways to express and enact that desired state.

### Terraform — run-on-demand workflow

How it works

* The developer writes HCL describing the AWS RDS instance.
* Running `terraform apply` calls the AWS API, creates the RDS instance, and stores resource metadata in a state file (for example `terraform.tfstate`).
* Terraform is declarative, but it only takes action when triggered by a human or CI automation.

Example (simplified Terraform):

```hcl theme={null}
# main.tf (simplified)
provider "aws" {
  region = "us-west-2"
}

resource "aws_db_instance" "postgres" {
  engine                  = "postgres"
  instance_class          = "db.t3.micro"
  allocated_storage       = 20
  backup_retention_period = 7
  name                    = "mydb"
}
```

Commands:

```bash theme={null}
$ terraform init
$ terraform apply
```

Typical effect

* Terraform provisions an RDS instance on AWS and records details in the state file.
* Subsequent changes are applied only when Terraform is executed again.

Drift example with Terraform

* If someone disables backups directly in the AWS Console, the real resource diverges from the Terraform configuration.
* Terraform will not automatically detect or remediate that drift until a person or automation runs `terraform plan` / `terraform apply`.

### Crossplane — continuous reconciliation inside Kubernetes

How it works

* Crossplane runs as one or more controllers inside your Kubernetes cluster.
* You express external cloud resources as Kubernetes custom resources (YAML manifests).
* Crossplane’s controllers observe these resources and call cloud provider APIs to create and maintain the external resources, continuously reconciling any drift.

Example (simplified desired-state manifest):

```yaml theme={null}
# db.yaml (simplified intent)
apiVersion: example.org/v1alpha1
kind: PostgreSQLInstance
metadata:
  name: mydb
spec:
  engine: postgres
  size: small
  backups: enabled
```

Apply it:

```bash theme={null}
$ kubectl apply -f db.yaml
```

What Crossplane does differently

* Acts like any Kubernetes controller: it watches the declared resource and keeps the external provider in the declared state.
* Detects and remediates drift (for example, if backups are disabled in the AWS Console, Crossplane will restore backups to match `backups: enabled`).
* Enables GitOps and Kubernetes-native workflows for cloud infrastructure (deploy via `kubectl`, CI/CD, or Git reconciliation).

The core difference: Terraform declares desired state and requires an explicit run to apply changes. Crossplane declares desired state as Kubernetes resources and runs continuously inside the cluster to automatically reconcile external resources to match that state.

## When to use each tool

* Use Terraform when:
  * You want ad-hoc or human-triggered provisioning.
  * Your team prefers direct IaC runs (CLI/CI pipelines) and centralized state files.
  * You manage a variety of infrastructure outside Kubernetes and prefer Terraform’s provider ecosystem.

* Use Crossplane when:
  * You want Kubernetes-native, declarative management of cloud resources.
  * Continuous reconciliation and automatic drift remediation are required.
  * You want to adopt GitOps patterns where infrastructure is reconciled by controllers in the cluster.

## Quick comparison

| Aspect | Terraform | Crossplane |
| - | - | - |
| Execution model | Run-on-demand (`terraform apply`) | Continuous controllers inside Kubernetes |
| Desired-state expressed as | HCL files | Kubernetes YAML custom resources |
| Drift detection/remediation | Manual / automation-triggered | Automatic, continuous reconciliation |
| State storage | `terraform.tfstate` (local or remote backend) | Kubernetes API + provider-managed state (via controllers) |
| Best fit | Direct IaC pipelines, ad-hoc provisioning | Kubernetes-first teams, GitOps, continuous enforcement |

## Further reading and references

* Terraform: [https://www.terraform.io](https://www.terraform.io)
* Crossplane: [https://crossplane.io](https://crossplane.io)
* Kubernetes concepts: [https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/](https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/)
* kubectl reference: [https://kubernetes.io/docs/reference/kubectl/overview/](https://kubernetes.io/docs/reference/kubectl/overview/)
* Amazon RDS: [https://aws.amazon.com/rds/](https://aws.amazon.com/rds/)
* PostgreSQL: [https://www.postgresql.org/](https://www.postgresql.org/)
* YAML specification: [https://yaml.org/spec/1.2/spec.html](https://yaml.org/spec/1.2/spec.html)

Summary

* Terraform provides a powerful, provider-rich IaC workflow that runs when executed.
* Crossplane brings infrastructure into the Kubernetes reconciliation loop so your cluster continuously enforces the desired state for external resources.

> [card] [Watch Video](https://learn.kodekloud.com/user/courses/devops-interview-prep/module/370000ef-b6bc-4986-8d29-0793ebb2c9e7/lesson/ffce1621-82c6-4027-bfe1-8a9129d623dd)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.