Import state verification in Terraform - Time & Space Complexity
When Terraform imports resources, it checks the current state to match real infrastructure.
We want to know how the time needed grows as we import more resources.
Analyze the time complexity of the following import verification steps.
resource "aws_instance" "example" {
# resource config
}
terraform import aws_instance.example i-1234567890abcdef0
terraform state show aws_instance.example
This sequence imports a resource and verifies its state matches the real instance.
Look at what happens repeatedly when importing multiple resources.
- Primary operation: API call to fetch resource details for each import
- How many times: Once per resource imported
Each resource import triggers one API call to verify state.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 API calls |
| 100 | 100 API calls |
| 1000 | 1000 API calls |
Pattern observation: The number of API calls grows directly with the number of resources.
Time Complexity: O(n)
This means the time to verify state grows in a straight line as you add more resources.
[X] Wrong: "Importing many resources only takes a fixed amount of time regardless of count."
[OK] Correct: Each resource needs a separate check, so time grows with how many you import.
Understanding how operations scale helps you plan and explain infrastructure changes clearly.
"What if Terraform cached resource states locally? How would that affect the time complexity of import verification?"
Practice
terraform import in Terraform?Solution
Step 1: Understand Terraform state
Terraform state tracks resources it manages. Existing resources outside Terraform are unknown to it.Step 2: Role of terraform import
Theterraform importcommand adds existing resources to Terraform state without creating or deleting them.Final Answer:
To bring existing cloud resources into Terraform state management -> Option DQuick Check:
Import = Add existing resource to state [OK]
- Thinking import creates new resources
- Confusing import with terraform apply
- Assuming import deletes resources
i-1234567890abcdef0 into Terraform resource aws_instance.example?Solution
Step 1: Identify correct command order
The syntax isterraform import <resource_address> <resource_id>.Step 2: Match resource address and ID
Resource address isaws_instance.example, ID isi-1234567890abcdef0. So terraform import aws_instance.example i-1234567890abcdef0 matches syntax correctly.Final Answer:
terraform import aws_instance.example i-1234567890abcdef0 -> Option BQuick Check:
Import syntax = terraform import resource_address resource_id [OK]
- Swapping resource address and ID
- Using dot in wrong place
- Missing resource type or name
terraform import aws_s3_bucket.mybucket my-bucket-name, what will terraform plan show if the Terraform configuration matches the imported bucket exactly?Solution
Step 1: Understand import effect
Import adds existing resource to state but does not change configuration files.Step 2: Compare state and config
If configuration matches the imported resource exactly,terraform planshows no changes needed.Final Answer:
No changes. Terraform state matches configuration. -> Option CQuick Check:
Matching config and state = no changes [OK]
- Expecting terraform plan to create or delete after import
- Confusing import with apply
- Ignoring configuration mismatch
terraform import aws_db_instance.db1 db-1234 but terraform plan shows the instance will be destroyed and recreated. What is the most likely cause?Solution
Step 1: Analyze import and plan mismatch
If plan wants to destroy and recreate, it means config differs from imported resource state.Step 2: Confirm configuration matches resource
Mismatch in attributes causes Terraform to think resource needs replacement.Final Answer:
The Terraform configuration does not match the imported resource attributes -> Option AQuick Check:
Config mismatch causes resource replacement plan [OK]
- Assuming import ID error always causes plan changes
- Blaming Terraform version without checking config
- Ignoring attribute differences
Solution
Step 1: Import each existing security group
Useterraform importwith correct resource address and IDs for each group.Step 2: Verify with terraform plan
Runterraform planto check if imported state matches configuration and no unexpected changes occur.Final Answer:
Run terraform import for each security group, then run terraform plan to verify state matches configuration -> Option AQuick Check:
Import each resource, then verify with plan [OK]
- Skipping plan after import
- Trying to create duplicates with apply
- Manually editing state file causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
