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
Terraform's Declarative Approach
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create a virtual private cloud (VPC) with a specific CIDR block to isolate your resources.
🎯 Goal: Build a Terraform configuration that declares a VPC resource with a fixed CIDR block using Terraform's declarative syntax.
📋 What You'll Learn
Create a Terraform configuration file named main.tf.
Declare a resource of type aws_vpc named main_vpc.
Set the cidr_block attribute of the VPC to 10.0.0.0/16.
Use Terraform's declarative syntax to define the resource.
💡 Why This Matters
🌍 Real World
Terraform is widely used to manage cloud infrastructure by declaring resources in code, making deployments repeatable and consistent.
💼 Career
Understanding Terraform's declarative syntax is essential for cloud engineers and DevOps professionals to automate infrastructure provisioning.
Progress0 / 4 steps
1
Declare the AWS provider
Write a Terraform block to declare the aws provider with the region set to us-east-1.
Terraform
Hint
The provider block tells Terraform which cloud provider and region to use.
2
Declare the VPC resource
Add a Terraform resource block to declare an aws_vpc resource named main_vpc.
Terraform
Hint
Use the resource keyword followed by the resource type and name.
3
Set the CIDR block for the VPC
Inside the aws_vpc resource named main_vpc, set the cidr_block attribute to 10.0.0.0/16.
Terraform
Hint
The cidr_block defines the IP address range for the VPC.
4
Add a tag to the VPC
Add a tags block inside the aws_vpc resource named main_vpc with a tag Name set to MyVPC.
Terraform
Hint
Tags help identify resources in the cloud console.
Practice
(1/5)
1. What does Terraform's declarative approach mean? terraform apply will create resources based on what you specify, not how to create them.
easy
A. You describe the desired state of infrastructure, not the steps to create it.
B. You write scripts that run commands step-by-step to build infrastructure.
C. You manually create resources in the cloud console and Terraform tracks them.
D. You use Terraform only to delete resources, not create them.
Solution
Step 1: Understand declarative vs imperative
Declarative means describing the end state, while imperative means describing the steps to get there.
Step 2: Apply to Terraform
Terraform uses declarative approach by letting you write configuration files that describe what you want, not how to do it.
Final Answer:
You describe the desired state of infrastructure, not the steps to create it. -> Option A
Quick Check:
Declarative = Describe desired state [OK]
Hint: Declarative means say what, not how [OK]
Common Mistakes:
Confusing declarative with imperative scripting
Thinking Terraform runs commands step-by-step
Believing Terraform only deletes resources
2. Which Terraform configuration snippet correctly declares an AWS S3 bucket named "mybucket"?
easy
A. resource aws_s3_bucket mybucket { bucket_name = "mybucket" }
B. create aws_s3_bucket mybucket { name = "mybucket" }
C. aws_s3_bucket "mybucket" { bucket_name = "mybucket" }
D. resource "aws_s3_bucket" "mybucket" { bucket = "mybucket" }
Terraform resource blocks start with 'resource', then resource type in quotes, then resource name in quotes, followed by braces with arguments.
Step 2: Match correct attribute names
The attribute to name an S3 bucket is 'bucket', not 'bucket_name'. resource "aws_s3_bucket" "mybucket" { bucket = "mybucket" } matches correct syntax and attribute.
What will Terraform do when you run terraform apply for the first time?
medium
A. It will delete any existing EC2 instances named example.
B. It will only plan the changes but not create any resources.
C. It will create an AWS EC2 instance with the specified AMI and instance type.
D. It will throw a syntax error due to missing provider block.
Solution
Step 1: Understand resource declaration effect
The resource block declares an EC2 instance with given AMI and type. Terraform will create it on apply.
Step 2: Consider first apply behavior
On first apply, Terraform creates resources to match the declared state. Missing provider block is not a syntax error but requires provider configuration elsewhere.
Final Answer:
It will create an AWS EC2 instance with the specified AMI and instance type. -> Option C
Quick Check:
First apply creates declared resources [OK]
Hint: terraform apply creates resources declared first time [OK]
But Terraform apply fails with an error about bucket name already existing. What is the best fix?
medium
A. Change the bucket name to a unique one because S3 bucket names must be globally unique.
B. Remove the acl attribute to fix the error.
C. Rename the resource block from "bucket" to "mybucket".
D. Run terraform destroy before apply to clear existing buckets.
Solution
Step 1: Understand S3 bucket naming rules
S3 bucket names must be globally unique across all AWS accounts.
Step 2: Analyze error cause
The error means the bucket name "mybucket" is already taken by someone else, so Terraform cannot create it.
Step 3: Choose fix
Changing the bucket name to a unique one solves the problem. Other options do not address uniqueness.
Final Answer:
Change the bucket name to a unique one because S3 bucket names must be globally unique. -> Option A
Quick Check:
S3 bucket names must be unique globally [OK]
Hint: S3 bucket names must be unique globally [OK]
Common Mistakes:
Thinking acl attribute causes name conflict
Renaming resource block does not change bucket name
Destroying resources unnecessarily
5. You want to manage a set of AWS EC2 instances with Terraform declaratively. You have a list of instance names and want to create one instance per name. Which Terraform feature best fits this declarative approach?
hard
A. Manually create each instance resource block with a unique name.
B. Use a for_each meta-argument on the resource block with the list of names.
C. Write a shell script to loop and run terraform apply multiple times.
D. Use a count meta-argument with a fixed number and hardcoded names.
Solution
Step 1: Understand declarative resource creation for multiple items
Terraform's for_each lets you declare multiple instances based on a collection, matching the declarative style.
Step 2: Evaluate options
Shell scripts and manual blocks are imperative or repetitive, not declarative. Using count with hardcoded names is less flexible than for_each.
Final Answer:
Use a for_each meta-argument on the resource block with the list of names. -> Option B
Quick Check:
for_each creates resources declaratively from collections [OK]