The terraform import command is a powerful feature in Terraform that allows you to bring existing infrastructure resources under Terraform management. When you have resources that were created manually through a cloud provider's console, CLI, or other tools outside of Terraform, the import command …The terraform import command is a powerful feature in Terraform that allows you to bring existing infrastructure resources under Terraform management. When you have resources that were created manually through a cloud provider's console, CLI, or other tools outside of Terraform, the import command enables you to incorporate them into your Terraform state file.
The basic syntax is: terraform import <resource_address> <resource_id>
For example, to import an AWS EC2 instance, you would write: terraform import aws_instance.my_server i-1234567890abcdef0
Before running the import command, you must first write a corresponding resource configuration block in your Terraform configuration files. The import command only updates the state file; it does not generate configuration code for you. This means you need to manually create the resource block that matches the imported resource's attributes.
Key considerations when using terraform import:
1. State Management: The command adds the resource to your terraform.tfstate file, establishing the link between your configuration and the actual infrastructure.
2. Configuration Matching: After importing, you should run terraform plan to verify your written configuration matches the actual resource. Any differences will appear as changes Terraform wants to make.
3. Resource Identification: Each provider has specific resource ID formats. Consult the provider documentation for the correct ID format.
4. Dependencies: Imported resources may have dependencies that also need importing or configuring.
5. Limitations: Some resources have complex structures or nested components that require multiple import commands.
Starting with Terraform 1.5, you can also use import blocks within your configuration files for a more declarative approach. This method allows you to define imports in code and generate configuration automatically using terraform plan -generate-config-out=generated.tf.
The import functionality is essential for adopting Terraform in environments with pre-existing infrastructure, enabling teams to gradually transition to infrastructure as code practices.
The Terraform Import Command
Why It Is Important
The terraform import command is a critical tool for managing infrastructure that already exists outside of Terraform's control. In real-world scenarios, organizations often have pre-existing infrastructure that was created manually through cloud consoles, CLI tools, or other automation methods. The import command bridges this gap by allowing you to bring these resources under Terraform management, enabling consistent infrastructure-as-code practices across your entire environment.
What Is Terraform Import?
The terraform import command is used to import existing infrastructure resources into your Terraform state file. This allows Terraform to begin managing resources that were created outside of Terraform. The command associates an existing resource with a Terraform resource configuration that you define in your code.
Key characteristics: • It only imports resources into the state file • It does NOT generate configuration code automatically (in older versions) • You must write the corresponding resource block in your configuration files • Each resource must be imported individually
How It Works
The basic syntax is: terraform import [options] ADDRESS ID
Where: • ADDRESS is the resource address in your Terraform configuration (e.g., aws_instance.my_server) • ID is the provider-specific identifier for the existing resource
Step-by-step process: 1. Write an empty or partial resource block in your configuration file 2. Run the terraform import command with the resource address and actual resource ID 3. Terraform adds the resource to the state file 4. Run terraform plan to see what attributes need to be added to your configuration 5. Update your configuration to match the actual resource settings 6. Run terraform plan again to verify no changes are needed
Starting with Terraform 1.5, you can use an import block in your configuration:
import { to = aws_instance.web id = "i-1234567890abcdef0"}
This declarative approach allows you to plan imports before executing them and can generate configuration using terraform plan -generate-config-out=generated.tf
Important Limitations
• Import only affects the state file, not the configuration files • You cannot import the same resource twice • Some resources may not support import • Complex resources may require manual configuration adjustments after import • Sensitive values are stored in state but may not appear in plan output
Exam Tips: Answering Questions on The Terraform Import Command
1. Remember the syntax order: The command follows terraform import ADDRESS ID - address comes before the resource ID
2. Configuration requirement: Before running import, you must have a corresponding resource block in your configuration files. The import command does not create configuration for you in traditional usage
3. State-only operation: Understand that import only modifies the state file. Your configuration files remain unchanged after running the command
4. Post-import steps: After importing, you typically need to run terraform plan to identify configuration attributes that need to be added to match the actual resource
5. Resource IDs vary: Each provider and resource type has its own ID format. For AWS EC2 instances, it is the instance ID. For other resources, it might be an ARN or name
6. One at a time: Traditional import handles one resource per command execution. For bulk imports, consider using the import block feature
7. Know the import block syntax: Be familiar with the newer import block feature introduced in Terraform 1.5, including the to and id arguments
8. Common exam scenarios: Questions often ask about what happens to existing resources, the order of operations, or what files are modified during import