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
Declarative vs Imperative Infrastructure as Code with Terraform
📖 Scenario: You are working as a cloud engineer. Your team wants to understand the difference between declarative and imperative ways of managing cloud infrastructure. You will create a simple Terraform configuration to declare a cloud resource, then add a variable to configure it, then use a loop to create multiple resources, and finally complete the configuration with an output.
🎯 Goal: Build a Terraform configuration that declares an AWS S3 bucket, uses a variable to set the bucket name prefix, creates multiple buckets using a loop, and outputs the bucket names. This will demonstrate declarative Infrastructure as Code (IaC) principles.
📋 What You'll Learn
Use Terraform syntax to declare resources
Create a variable to configure bucket name prefix
Use a for_each loop to create multiple buckets
Output the list of created bucket names
💡 Why This Matters
🌍 Real World
Cloud engineers use Terraform to declare and manage cloud resources in a clear, repeatable way. This project shows how to write simple, reusable Terraform code.
💼 Career
Understanding declarative IaC with Terraform is essential for roles like Cloud Engineer, DevOps Engineer, and Infrastructure Developer.
Progress0 / 4 steps
1
Declare a single AWS S3 bucket resource
Write a Terraform resource block named aws_s3_bucket with the resource name example_bucket. Set the bucket attribute to the exact string "my-unique-bucket-001". This declares one S3 bucket.
Terraform
Hint
Use resource "aws_s3_bucket" "example_bucket" { ... } and set bucket = "my-unique-bucket-001".
2
Add a variable for bucket name prefix
Create a Terraform variable block named bucket_prefix of type string. Set its default value to "my-unique-bucket". This variable will help configure bucket names.
Terraform
Hint
Use variable "bucket_prefix" { type = string default = "my-unique-bucket" }.
3
Use a for_each loop to create multiple buckets
Replace the single aws_s3_bucket resource with a resource named aws_s3_bucket and resource name multiple_buckets. Use for_each with the set {1, 2, 3}. Set the bucket attribute to the expression "${var.bucket_prefix}-${each.key}" to create three buckets with names like my-unique-bucket-1.
Terraform
Hint
Use for_each = toset([1, 2, 3]) and bucket = "${var.bucket_prefix}-${each.key}".
4
Add an output for the bucket names
Create a Terraform output block named bucket_names. Set its value to a list of all bucket names created by aws_s3_bucket.multiple_buckets. Use the expression [for b in aws_s3_bucket.multiple_buckets : b.bucket].
Terraform
Hint
Use output "bucket_names" { value = [for b in aws_s3_bucket.multiple_buckets : b.bucket] }.
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
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 A
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
Step 1: Identify declarative syntax
Terraform resource blocks declare the desired state, like the resource "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 B
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?