What if you could tell the cloud what you want and it just makes it happen perfectly every time?
Declarative vs imperative IaC in Terraform - When to Use Which
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine setting up a whole cloud network by typing every single command one by one, like telling someone step-by-step how to build a house without a blueprint.
This manual way is slow and easy to mess up. If you forget a step or make a typo, the whole setup can break, and fixing it means starting over or hunting for errors.
Declarative Infrastructure as Code lets you write what you want your cloud to look like, not how to build it. The system figures out the steps, making setup faster, safer, and repeatable.
create VM install software configure network start VM
resource "vm" { image = "ubuntu" network = "default" }
It makes managing cloud resources simple and reliable, so you can focus on your goals, not the details.
A company uses declarative IaC to launch hundreds of servers automatically for a big event, avoiding mistakes and saving hours of work.
Manual cloud setup is slow and error-prone.
Declarative IaC describes the desired state, not steps.
This approach makes cloud management faster and safer.
Practice
Solution
Step 1: Understand declarative IaC
Declarative IaC focuses on describing the final desired state of infrastructure without specifying how to achieve it.Step 2: Understand imperative IaC
Imperative IaC lists the exact commands or steps needed to create or change infrastructure.Final Answer:
Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it. -> Option AQuick Check:
Declarative = final state, Imperative = steps [OK]
- Confusing declarative with imperative steps
- Thinking declarative requires manual commands
- Assuming declarative only works for cloud
Solution
Step 1: Identify declarative syntax
Terraform resource blocks declare the desired state, like theresource "aws_instance" "web" { ... }block.Step 2: Compare with imperative styles
Options B, C, and D show commands or logic steps, which are imperative, not declarative.Final Answer:
The Terraform resource block syntax is declarative. -> Option BQuick Check:
Terraform resource block = declarative [OK]
- Choosing imperative command syntax as declarative
- Confusing function calls with resource declarations
- Ignoring the resource block structure
terraform apply if the AWS instance does not exist yet?
resource "aws_instance" "example" {
ami = "ami-abc123"
instance_type = "t2.micro"
}Solution
Step 1: Understand Terraform apply behavior
Terraform compares the current state with the desired state defined in the resource block.Step 2: Predict action if resource missing
If the AWS instance does not exist, Terraform will create it to match the declared state.Final Answer:
Terraform will create a new AWS instance with the specified AMI and instance type. -> Option CQuick Check:
Missing resource = Terraform creates it [OK]
- Thinking Terraform deletes resources by default
- Expecting manual commands during apply
- Assuming syntax error in valid resource block
terraform apply fails with an error:
resource "aws_instance" "web" {
ami = "ami-123"
instance_type = t2.micro
}
What is the likely cause of the error?Solution
Step 1: Check syntax of resource attributes
Terraform requires string values to be in quotes. The instance_type value is missing quotes.Step 2: Identify error cause
Missing quotes cause a syntax error, preventing Terraform from parsing the file.Final Answer:
Missing quotes around the instance_type value causes a syntax error. -> Option DQuick Check:
Strings need quotes in Terraform [OK]
- Ignoring missing quotes around strings
- Thinking resource names cause syntax errors
- Expecting commands inside resource blocks
Solution
Step 1: Understand declarative IaC goal
Declarative IaC means describing the final infrastructure state in configuration files.Step 2: Identify Terraform usage
Terraform uses configuration files to declare resources, then applies changes automatically.Final Answer:
Write a Terraform configuration file describing the desired resources and runterraform apply. -> Option AQuick Check:
Declarative IaC = config files + apply [OK]
- Confusing declarative IaC with manual scripting
- Thinking manual cloud console use is declarative
- Using imperative CLI commands instead of config files
