Resource block syntax in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time to create resources grows when we add more resource blocks in Terraform.
How does the number of resources affect the total work Terraform does?
Analyze the time complexity of creating multiple similar resources using resource blocks.
resource "aws_instance" "example" {
count = var.instance_count
ami = "ami-123456"
instance_type = "t2.micro"
}
This code creates a number of AWS instances based on the variable instance_count.
Each resource block creates one instance.
- Primary operation: API call to create one AWS instance.
- How many times: Equal to the value of
instance_count.
When you increase the number of instances, the number of API calls grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the number of instances.
Time Complexity: O(n)
This means the time to create resources grows linearly with the number of resource blocks.
[X] Wrong: "Adding more resource blocks will not increase the total time much because they run in parallel."
[OK] Correct: While some operations may run in parallel, each resource still requires its own API call and provisioning time, so total work grows with the number of resources.
Understanding how resource count affects deployment time helps you design efficient infrastructure and explain your choices clearly in discussions.
"What if we used modules to create resources instead of resource blocks? How would the time complexity change?"
Practice
Solution
Step 1: Understand resource block syntax
A resource block starts with the keywordresource, followed by the resource type in quotes, then the resource name in quotes.Step 2: Match the correct syntax
resource "aws_instance" "example" { }correctly usesresource "aws_instance" "example" { }, which matches the required syntax.Final Answer:
resource "aws_instance" "example" { }-> Option DQuick Check:
Resource block syntax = resource "type" "name" { } [OK]
- Omitting quotes around type or name
- Swapping type and name order
- Using braces incorrectly
Solution
Step 1: Identify correct property syntax
Terraform uses=to assign values, and string values must be in quotes.Step 2: Check each option
instance_type = "t2.micro"usesinstance_type = "t2.micro", which is correct.instance_type = t2.micromisses quotes, B and C use invalid assignment symbols.Final Answer:
instance_type = "t2.micro" -> Option CQuick Check:
Property assignment uses = and quotes for strings [OK]
- Forgetting quotes around string values
- Using colon or arrow instead of =
- Missing equal sign
resource "aws_s3_bucket" "mybucket" {
bucket = "my-unique-bucket-123"
acl = "private"
}Solution
Step 1: Identify resource type and name
The resource type isaws_s3_bucket, and the resource name ismybucket. The bucket property sets the actual bucket name.Step 2: Understand properties
Thebucketproperty sets the bucket's real name to "my-unique-bucket-123". Theaclis set to "private", meaning no public access.Final Answer:
An S3 bucket named "my-unique-bucket-123" with private access -> Option BQuick Check:
Bucket name = property value, ACL = private [OK]
- Confusing resource name with actual bucket name
- Assuming default ACL is public
- Ignoring property values inside the block
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = t2.micro
}Solution
Step 1: Check property value syntax
Theinstance_typeproperty valuet2.microis missing quotes, which is required for string values.Step 2: Verify block structure
The resource block has correct braces and resource type/name, so no errors there.Final Answer:
Missing quotes around the instance_type value -> Option AQuick Check:
String values must be quoted [OK]
- Forgetting quotes around string properties
- Assuming braces are optional
- Mixing resource type and name order
Solution
Step 1: Understand resource naming and duplication
Each resource block must have a unique name.resource "aws_instance" "web1" { ami = "ami-abc123" instance_type = "t2.micro" } resource "aws_instance" "web2" { ami = "ami-abc123" instance_type = "t2.micro" }defines two separate resources with different names.Step 2: Evaluate other options
resource "aws_instance" "web" { count = 2 ami = "ami-abc123" instance_type = "t2.micro" }uses count but does not differentiate names.resource "aws_instance" "web" { ami = "ami-abc123" instance_type = "t2.micro" name = ["web1", "web2"] }tries to assign a list to name property, which is invalid.resource "aws_instance" "web" { ami = "ami-abc123" instance_type = "t2.micro" for_each = ["web1", "web2"] }uses for_each incorrectly without mapping names.Final Answer:
Define two separate resource blocks with unique names -> Option AQuick Check:
Unique resource names needed for multiple instances [OK]
- Using count without unique names
- Assigning list to single property
- Misusing for_each without proper keys
