0
0
Terraformcloud~5 mins

Import limitations and considerations in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you have existing cloud resources that you want to manage with Terraform. Importing lets Terraform track these resources. But there are limits and things to watch out for to avoid problems.
When you have a cloud server already running and want Terraform to manage it without recreating it
When you want to bring existing storage buckets under Terraform control
When you need to manage resources created manually or by other tools
When migrating infrastructure management to Terraform without downtime
When you want to avoid deleting and recreating resources during Terraform setup
Commands
This command imports an existing AWS EC2 instance with ID i-0abcd1234efgh5678 into Terraform state under the resource name aws_instance.my_server. It lets Terraform track this instance without changing it.
Terminal
terraform import aws_instance.my_server i-0abcd1234efgh5678
Expected OutputExpected
aws_instance.my_server: Importing from ID "i-0abcd1234efgh5678"... aws_instance.my_server: Import prepared! Prepared aws_instance for import aws_instance.my_server: Refreshing state... [id=i-0abcd1234efgh5678] Import successful! The resources that were imported are now in your Terraform state and will be managed by Terraform.
This command shows what Terraform will do next. After import, it helps verify if the imported resource matches your Terraform configuration or if changes will be applied.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date.
Key Concept

If you remember nothing else from this pattern, remember: Terraform import only adds existing resources to state; it does not create configuration or change resources automatically.

Common Mistakes
Running terraform import without having the matching resource block in the configuration file
Terraform imports the resource into state but cannot manage it properly without the configuration, leading to errors or confusion.
Always write the resource block in your Terraform files before importing the resource.
Expecting terraform import to create or modify resources
Import only adds existing resources to Terraform state; it does not create or change resources.
Use terraform import only for existing resources; use terraform apply to create or modify.
Not verifying the imported resource with terraform plan after import
The imported resource may differ from the configuration, causing unexpected changes on apply.
Always run terraform plan after import to check for differences.
Summary
Use terraform import to add existing resources to Terraform state without recreating them.
Always have the matching resource configuration in your Terraform files before importing.
Run terraform plan after import to verify the imported resource matches your configuration.