Code review for infrastructure changes in Terraform - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When reviewing infrastructure code changes, it's important to understand how the time to check and apply these changes grows as the code grows.
We want to know: how does the review and apply process scale with the number of resources changed?
Analyze the time complexity of reviewing and applying changes for multiple Terraform resources.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
resource "aws_security_group" "example" {
count = var.sg_count
name = "example-sg-${count.index}"
description = "Example security group"
}
This code creates multiple instances and security groups based on input counts.
Each resource block triggers API calls to create or update resources.
- Primary operation: API calls to provision or update each resource instance.
- How many times: Once per resource instance, repeated for each count.
As the number of instances or security groups increases, the number of API calls grows proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | About 10 calls for instances + 10 for security groups = 20 |
| 100 | About 100 calls for instances + 100 for security groups = 200 |
| 1000 | About 1000 calls for instances + 1000 for security groups = 2000 |
Pattern observation: The total operations grow directly with the number of resources defined.
Time Complexity: O(n)
This means the time to review and apply changes grows linearly with the number of resources.
[X] Wrong: "Reviewing or applying changes takes the same time no matter how many resources are changed."
[OK] Correct: Each resource requires separate API calls and checks, so more resources mean more work and time.
Understanding how infrastructure changes scale helps you plan and communicate about deployments clearly and confidently.
"What if we used modules to group resources instead of individual resource blocks? How would that affect the time complexity?"
Practice
terraform plan before applying changes?Solution
Step 1: Understand the role of
This command shows what changes Terraform will perform without making any actual changes.terraform planStep 2: Differentiate from other commands
terraform applymakes changes, whileterraform planpreviews them safely.Final Answer:
To preview the changes Terraform will make to the infrastructure -> Option BQuick Check:
Preview changes = terraform plan [OK]
- Confusing plan with apply
- Thinking plan deletes resources
- Assuming plan creates backups
Solution
Step 1: Identify the initialization command
terraform initsets up the working directory by downloading providers and preparing backend.Step 2: Verify other options
Commands liketerraform start,terraform setup, andterraform configuredo not exist in Terraform CLI.Final Answer:
terraform init -> Option AQuick Check:
Initialize = terraform init [OK]
- Using non-existent commands
- Confusing init with apply
- Assuming configure is a Terraform command
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
}
output "instance_id" {
value = aws_instance.example.id
}What will
terraform apply output after successful deployment?Solution
Step 1: Understand the output block
The output namedinstance_idreturns the ID of the created AWS instance resource.Step 2: Confirm output value
The value is set toaws_instance.example.id, which is the unique instance ID assigned by AWS.Final Answer:
The ID of the created AWS instance -> Option AQuick Check:
Output shows instance ID = The ID of the created AWS instance [OK]
- Confusing output value with input AMI
- Expecting instance type as output
- Thinking output block is missing or invalid
resource "aws_s3_bucket" "my_bucket" {
bucket = "my-unique-bucket-name"
acl = "public-read"
}What is the main concern during code review before applying?
Solution
Step 1: Analyze the ACL setting
The ACL is set topublic-read, which allows anyone on the internet to read bucket contents.Step 2: Consider security best practices
Making buckets public can expose sensitive data; this should be reviewed carefully before applying.Final Answer:
The ACL setting makes the bucket publicly readable, which may be a security risk -> Option DQuick Check:
Public ACL = security risk [OK]
- Ignoring security implications of ACL
- Assuming bucket name uniqueness is the main issue
- Thinking region is mandatory in resource block
Solution
Step 1: Understand collaboration best practices
Sharingterraform planoutput in pull requests allows the team to see proposed changes safely before applying.Step 2: Evaluate other options
Applying changes without review or sending raw files lacks safety and clarity; notifying after applying is risky.Final Answer:
Shareterraform planoutput in a pull request for team feedback -> Option CQuick Check:
Plan + PR = safe collaboration [OK]
- Skipping review before apply
- Sharing raw files without context
- Applying changes before team agreement
