Terraform interview question · Most asked
How do you bring existing infrastructure under Terraform management?
Summary
Use import blocks (Terraform 1.5+) in configuration, which are planned and reviewed like any change, optionally with terraform plan -generate-config-out=generated.tf to generate a starting configuration for the imported resources. The older terraform import CLI command imports one resource at a time into state without configuration.
Longer Answer
Workflow: write import blocks with the target address and real ID, generate or hand-write the resource configuration, run plan until it shows no changes (meaning configuration matches reality), then apply to record it in state, and clean up the generated code into modules. Tools such as Terraformer or provider-specific exporters help with large estates. Import is also how you recover when a resource exists but state lost track of it.
Key Points
- import blocks (1.5+) are reviewable.
- Generate config with -generate-config-out.
- Iterate until plan shows no changes.
- Clean up into modules.
More most-asked Terraform questions
- How do you handle secrets in Terraform?
- How would you run Terraform in CI/CD?
- What is Terraform state, why is it needed, and where should it be stored?
- count or for_each: when do you use each?
- Walk through terraform init, plan and apply. What does each do?
- What is Terraform, and how does it work at a high level?
- What are Terraform modules, and how do you design good ones?
- A terraform plan wants to destroy and recreate a database. What do you do?