0
0
Terraformcloud~5 mins

Declarative vs imperative IaC in Terraform - CLI Comparison

Choose your learning style9 modes available
Introduction
When managing cloud resources, you can tell the system what you want (declarative) or how to do it step-by-step (imperative). Terraform uses the declarative way, which means you describe the desired state and it figures out the steps to get there.
When you want to create or update cloud resources by describing what they should look like, not how to create them.
When you want your infrastructure setup to be easy to read and maintain by others.
When you want to avoid mistakes from manual step-by-step commands.
When you want to track changes to your infrastructure over time.
When you want to automate cloud resource management safely and predictably.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example_bucket" {
  bucket = "my-example-bucket-terraform-12345"
  acl    = "private"
}

This Terraform file declares a provider (AWS) and a resource (an S3 bucket). You describe the bucket you want, and Terraform will create it for you. You do not write commands to create the bucket; you just say what you want.

Commands
This command sets up Terraform in your folder by downloading the AWS provider plugin. It prepares Terraform to work with AWS.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do to reach the desired state described in the config file. It does not change anything yet.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_s3_bucket.example_bucket will be created + resource "aws_s3_bucket" "example_bucket" { + acl = "private" + bucket = "my-example-bucket-terraform-12345" + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create the S3 bucket as described. The -auto-approve flag skips the manual confirmation step.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Creating... aws_s3_bucket.example_bucket: Creation complete after 2s [id=my-example-bucket-terraform-12345] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without asking for confirmation
This command removes the S3 bucket created by Terraform. It cleans up the resources to avoid extra costs.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_s3_bucket.example_bucket: Destroying... aws_s3_bucket.example_bucket: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.
-auto-approve - Automatically approve the destroy without asking for confirmation
Key Concept

If you remember nothing else, remember: declarative IaC means you describe what you want, and the tool figures out how to do it.

Common Mistakes
Trying to write step-by-step commands inside the Terraform config file.
Terraform config files only describe the desired state, not the steps to get there.
Write resource blocks that describe the final infrastructure you want, and use Terraform commands to apply changes.
Running terraform apply without running terraform plan first.
You might apply unexpected changes without reviewing them first.
Always run terraform plan to see what changes will happen before applying.
Summary
Terraform uses declarative IaC to describe the desired cloud resources.
You write config files that say what you want, not how to do it.
Commands like terraform plan and terraform apply help you see and make changes safely.