Bird
Raised Fist0
Terraformcloud~20 mins

Declarative vs imperative IaC in Terraform - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
IaC Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Declarative Infrastructure as Code

Which statement best describes the declarative approach in Infrastructure as Code (IaC)?

AYou write code that specifies the desired end state of the infrastructure, and the tool figures out how to achieve it.
BYou write step-by-step commands to create and configure each resource manually.
CYou must manually run each command in sequence to build the infrastructure.
DYou write scripts that only describe the current state without any changes.
Attempts:
2 left
💡 Hint

Think about whether you describe the final setup or the steps to get there.

❓ Architecture
intermediate
2:00remaining
Identifying Imperative IaC Characteristics

Which of the following Terraform configurations shows an imperative style rather than declarative?

Note: Terraform is declarative by design, but consider which option mimics imperative behavior.

AUsing variables and outputs to parameterize and share data between modules.
BDefining a resource block with desired attributes and letting Terraform manage creation.
CUsing <code>provisioner "local-exec" { command = "echo Setup" }</code> to run shell commands during resource creation.
DWriting a Terraform module that describes a reusable infrastructure component.
Attempts:
2 left
💡 Hint

Imperative style involves explicit commands or scripts executed step-by-step.

❓ service_behavior
advanced
2:00remaining
Terraform Plan Behavior in Declarative IaC

What does the terraform plan command do in the context of declarative Infrastructure as Code?

AIt deletes all existing resources before creating new ones.
BIt immediately applies changes to the cloud infrastructure without confirmation.
CIt only validates the syntax of the Terraform files without checking infrastructure state.
DIt shows the exact steps Terraform will take to change the infrastructure to match the declared configuration.
Attempts:
2 left
💡 Hint

Think about how Terraform previews changes before applying them.

❓ security
advanced
2:00remaining
Security Implications of Imperative vs Declarative IaC

Which security risk is more likely when using imperative Infrastructure as Code compared to declarative?

AAutomatically enforcing consistent security policies across all resources.
BInadvertently skipping critical configuration steps leading to insecure resources.
CHaving a clear, auditable record of desired infrastructure state.
DUsing version control to track infrastructure changes.
Attempts:
2 left
💡 Hint

Consider how manual steps can cause mistakes.

✅ Best Practice
expert
3:00remaining
Choosing Between Declarative and Imperative IaC

You manage a large cloud infrastructure that changes frequently. Which approach is best to ensure reliable, repeatable deployments and why?

ADeclarative IaC, because it defines the desired state and lets the tool handle changes, reducing human error.
BImperative IaC, because writing scripts for each step gives full control over deployment order.
CImperative IaC, because it requires less upfront planning and is faster to write for small changes.
DDeclarative IaC, because it requires manual execution of each command to verify correctness.
Attempts:
2 left
💡 Hint

Think about scalability, repeatability, and error reduction.

Practice

(1/5)
1. What is the main difference between declarative and imperative Infrastructure as Code (IaC)?
easy
A. Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it.
B. Declarative IaC requires manual commands, while imperative IaC is fully automatic.
C. Declarative IaC is only for cloud, imperative IaC is for on-premises.
D. Declarative IaC uses scripts, imperative IaC uses configuration files.

Solution

  1. Step 1: Understand declarative IaC

    Declarative IaC focuses on describing the final desired state of infrastructure without specifying how to achieve it.
  2. Step 2: Understand imperative IaC

    Imperative IaC lists the exact commands or steps needed to create or change infrastructure.
  3. Final Answer:

    Declarative IaC describes the desired end state, while imperative IaC specifies exact steps to reach it. -> Option A
  4. Quick Check:

    Declarative = final state, Imperative = steps [OK]
Hint: Declarative = what you want; imperative = how to do it [OK]
Common Mistakes:
  • Confusing declarative with imperative steps
  • Thinking declarative requires manual commands
  • Assuming declarative only works for cloud
2. Which of the following Terraform code snippets correctly shows a declarative resource definition?
easy
A. create_instance("ami-123", "t2.micro")
B. resource "aws_instance" "web" { ami = "ami-123" instance_type = "t2.micro" }
C. if not instance_exists() then create_instance()
D. run_command("aws ec2 run-instances --image-id ami-123")

Solution

  1. Step 1: Identify declarative syntax

    Terraform resource blocks declare the desired state, like the resource "aws_instance" "web" { ... } block.
  2. Step 2: Compare with imperative styles

    Options B, C, and D show commands or logic steps, which are imperative, not declarative.
  3. Final Answer:

    The Terraform resource block syntax is declarative. -> Option B
  4. Quick Check:

    Terraform resource block = declarative [OK]
Hint: Terraform resource blocks are declarative descriptions [OK]
Common Mistakes:
  • Choosing imperative command syntax as declarative
  • Confusing function calls with resource declarations
  • Ignoring the resource block structure
3. Given this Terraform snippet, what will Terraform do when you run terraform apply if the AWS instance does not exist yet?
resource "aws_instance" "example" {
  ami           = "ami-abc123"
  instance_type = "t2.micro"
}
medium
A. Terraform will prompt for manual commands to create the instance.
B. Terraform will delete any existing instances and do nothing else.
C. Terraform will create a new AWS instance with the specified AMI and instance type.
D. Terraform will throw a syntax error because the resource block is incomplete.

Solution

  1. Step 1: Understand Terraform apply behavior

    Terraform compares the current state with the desired state defined in the resource block.
  2. Step 2: Predict action if resource missing

    If the AWS instance does not exist, Terraform will create it to match the declared state.
  3. Final Answer:

    Terraform will create a new AWS instance with the specified AMI and instance type. -> Option C
  4. Quick Check:

    Missing resource = Terraform creates it [OK]
Hint: Terraform creates missing resources to match declared state [OK]
Common Mistakes:
  • Thinking Terraform deletes resources by default
  • Expecting manual commands during apply
  • Assuming syntax error in valid resource block
4. You wrote this Terraform code but 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?
medium
A. Terraform requires an explicit create command inside the resource block.
B. The resource name "web" is invalid and must be changed.
C. The AMI ID "ami-123" is not allowed in Terraform.
D. Missing quotes around the instance_type value causes a syntax error.

Solution

  1. Step 1: Check syntax of resource attributes

    Terraform requires string values to be in quotes. The instance_type value is missing quotes.
  2. Step 2: Identify error cause

    Missing quotes cause a syntax error, preventing Terraform from parsing the file.
  3. Final Answer:

    Missing quotes around the instance_type value causes a syntax error. -> Option D
  4. Quick Check:

    Strings need quotes in Terraform [OK]
Hint: Always quote string values in Terraform resource blocks [OK]
Common Mistakes:
  • Ignoring missing quotes around strings
  • Thinking resource names cause syntax errors
  • Expecting commands inside resource blocks
5. You want to create a cloud infrastructure using Terraform declarative IaC. Which approach best fits this goal?
hard
A. Write a Terraform configuration file describing the desired resources and run terraform apply.
B. Write a script with commands to create each resource step-by-step manually.
C. Manually create resources in the cloud console, then import them into Terraform.
D. Use a shell script to run cloud CLI commands in order.

Solution

  1. Step 1: Understand declarative IaC goal

    Declarative IaC means describing the final infrastructure state in configuration files.
  2. Step 2: Identify Terraform usage

    Terraform uses configuration files to declare resources, then applies changes automatically.
  3. Final Answer:

    Write a Terraform configuration file describing the desired resources and run terraform apply. -> Option A
  4. Quick Check:

    Declarative IaC = config files + apply [OK]
Hint: Use Terraform config files, not manual scripts, for declarative IaC [OK]
Common Mistakes:
  • Confusing declarative IaC with manual scripting
  • Thinking manual cloud console use is declarative
  • Using imperative CLI commands instead of config files