Bulk import strategies in Terraform - Time & Space Complexity
When importing many resources in Terraform, it is important to understand how the time to complete the import grows as the number of resources increases.
We want to know how the number of import operations affects the total time taken.
Analyze the time complexity of the following operation sequence.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
# Import each instance by running a loop
# terraform import aws_instance.example[INDEX] i-INSTANCEID
This sequence defines multiple instances and imports each one individually using a loop over the count.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Terraform import command for each resource, which calls the cloud provider API to read resource state.
- How many times: Once per resource, so the number of imports equals the number of resources.
Each resource requires a separate import call, so as the number of resources grows, the total import operations grow proportionally.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 import calls |
| 100 | 100 import calls |
| 1000 | 1000 import calls |
Pattern observation: The number of import operations grows linearly with the number of resources.
Time Complexity: O(n)
This means the total time to import grows directly in proportion to how many resources you import.
[X] Wrong: "Importing multiple resources at once will take the same time as importing one resource."
[OK] Correct: Each resource import is a separate API call and takes time, so importing many resources adds up the time linearly.
Understanding how bulk operations scale helps you design efficient infrastructure workflows and shows you can think about system behavior as it grows.
"What if we batch multiple resource imports into a single API call? How would the time complexity change?"
Practice
Solution
Step 1: Understand Terraform import
Terraform import lets you bring existing cloud resources into Terraform state so you can manage them.Step 2: Recognize bulk import purpose
Bulk import strategies help import many resources quickly instead of one by one.Final Answer:
To efficiently bring many existing resources under Terraform management at once -> Option DQuick Check:
Bulk import = manage many existing resources [OK]
- Thinking bulk import creates new resources
- Confusing import with resource deletion
- Assuming import auto-generates Terraform code
Solution
Step 1: Recall Terraform commands
terraform apply creates or updates resources, terraform init initializes, terraform plan previews changes.Step 2: Identify import command
terraform import is the command that brings existing resources into Terraform state.Final Answer:
terraform import -> Option AQuick Check:
Import command = terraform import [OK]
- Using terraform apply to import
- Confusing init with import
- Trying terraform plan to import
for id in i-12345678 i-87654321; do terraform import aws_instance.example_$id $id; doneWhat will be the result after running this script?
Solution
Step 1: Analyze the for loop
The loop iterates over two instance IDs: i-12345678 and i-87654321.Step 2: Understand terraform import usage
Each iteration runs terraform import with resource name aws_instance.example_$id and the ID as argument, importing both instances.Final Answer:
Both instances will be imported with resource names aws_instance.example_i-12345678 and aws_instance.example_i-87654321 -> Option CQuick Check:
Loop imports both instances correctly [OK]
- Assuming only one import runs
- Thinking terraform creates new instances
- Believing script syntax is invalid
Solution
Step 1: Understand Terraform resource naming rules
Terraform resource names must use underscores (_) not dashes (-).Step 2: Identify cause of error
The error says resource name must not contain '-', so using dashes in resource names causes this error.Final Answer:
Using resource names with dashes (-) which Terraform disallows -> Option AQuick Check:
Resource names cannot have '-' [OK]
- Ignoring naming rules and using dashes
- Assuming missing init causes this error
- Blaming credentials for naming errors
Solution
Step 1: Understand bulk import best practice
Bulk import works best with automation scripts looping over resource IDs to import each resource.Step 2: Match Terraform configuration
Terraform requires matching resource blocks in config files for imported resources to manage them properly.Step 3: Evaluate options
Write a script that loops over VM IDs, runs terraform import for each, and maintains matching Terraform config files uses scripting and matching config, Run terraform import once with all VM IDs separated by commas is invalid syntax, Manually import each VM one by one using terraform import command is slow manual work, Create new Terraform resources and delete existing VMs to sync state deletes resources which is unsafe.Final Answer:
Write a script that loops over VM IDs, runs terraform import for each, and maintains matching Terraform config files -> Option BQuick Check:
Automate import with matching config files [OK]
- Trying to import multiple IDs in one command
- Manually importing many resources without automation
- Deleting resources instead of importing
Ready to prove it?
Pass the test and this pattern moves unknown
