Bird
Raised Fist0
Terraformcloud~5 mins

Terraform import command - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes you have resources created outside Terraform and want to manage them with Terraform. The terraform import command lets you bring those existing resources into Terraform's control without recreating them.
When you have a cloud server created manually and want Terraform to manage it.
When you want to track a database instance created by another team using Terraform.
When migrating existing infrastructure to Terraform for easier updates and version control.
When you accidentally created a resource outside Terraform and want to avoid deleting it.
When you want to add a resource created by a different tool into your Terraform state.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "example" {
  # The instance will be imported, so no need to define all attributes here
  # After import, you can add full configuration
}

This file sets up the AWS provider for Terraform to connect to AWS in the us-east-1 region.

The resource block defines an AWS EC2 instance named example. It is initially empty because the instance already exists and will be imported.

Commands
This command initializes the Terraform working directory, downloads provider plugins, and prepares Terraform to run.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command imports the existing AWS EC2 instance with ID i-0abcd1234efgh5678 into the Terraform resource aws_instance.example.
Terminal
terraform import aws_instance.example i-0abcd1234efgh5678
Expected OutputExpected
aws_instance.example: Importing from ID "i-0abcd1234efgh5678"... aws_instance.example: Import prepared! Prepared aws_instance for import aws_instance.example: 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 the changes Terraform will make. After import, it helps verify that Terraform matches the existing resource.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date. This means that Terraform's state matches the actual resource and no updates are needed.
Key Concept

If you remember nothing else from this pattern, remember: terraform import connects existing resources to Terraform's management without recreating them.

Common Mistakes
Trying to import a resource without defining a matching resource block in the Terraform configuration.
Terraform needs a resource block to map the imported resource to; without it, import fails.
First define the resource block in your .tf file with the correct resource type and name, then run terraform import.
Using the wrong resource ID or identifier when running terraform import.
Terraform cannot find the resource to import and the command fails.
Double-check the exact resource ID from your cloud provider or system before running terraform import.
Not running terraform init before terraform import.
Terraform is not initialized and does not have the necessary plugins to communicate with the provider.
Always run terraform init first to set up the working directory.
Summary
terraform init prepares Terraform to work with your cloud provider.
terraform import brings an existing resource into Terraform's state for management.
terraform plan verifies that Terraform's state matches the actual resource after import.

Practice

(1/5)
1. What is the main purpose of the terraform import command?
easy
A. To connect existing cloud resources to Terraform management
B. To create new resources in the cloud
C. To delete resources managed by Terraform
D. To update Terraform version

Solution

  1. Step 1: Understand the command's role

    The terraform import command is used to bring existing resources under Terraform's control without recreating them.
  2. Step 2: Differentiate from other commands

    Unlike commands that create or delete resources, import only connects existing ones to Terraform state.
  3. Final Answer:

    To connect existing cloud resources to Terraform management -> Option A
  4. Quick Check:

    Import = Connect existing resources [OK]
Hint: Import links existing resources to Terraform state [OK]
Common Mistakes:
  • Thinking import creates new resources
  • Confusing import with terraform apply
  • Assuming import deletes resources
2. Which of the following is the correct syntax to import an AWS S3 bucket named my-bucket into Terraform resource aws_s3_bucket.example?
easy
A. terraform import my-bucket aws_s3_bucket.example
B. terraform import aws_s3_bucket.example my-bucket
C. terraform import aws_s3_bucket.example aws_s3_bucket.my-bucket
D. terraform import example aws_s3_bucket.my-bucket

Solution

  1. Step 1: Identify correct command order

    The syntax is terraform import <resource_address> <resource_id>. Here, resource address is aws_s3_bucket.example and ID is my-bucket.
  2. Step 2: Match options to syntax

    terraform import aws_s3_bucket.example my-bucket matches the correct order: resource address first, then resource ID.
  3. Final Answer:

    terraform import aws_s3_bucket.example my-bucket -> Option B
  4. Quick Check:

    Import syntax = resource_address resource_id [OK]
Hint: Remember: import <resource_address> <resource_id> [OK]
Common Mistakes:
  • Swapping resource address and ID
  • Using resource type instead of address
  • Adding extra prefixes to resource ID
3. Given this Terraform import command:
terraform import aws_instance.web i-1234567890abcdef0
What will Terraform do after running this command?
medium
A. Create a new AWS instance with ID i-1234567890abcdef0
B. Delete the AWS instance i-1234567890abcdef0
C. Add the existing AWS instance i-1234567890abcdef0 to Terraform state under aws_instance.web
D. Update the AWS instance i-1234567890abcdef0 with Terraform configuration

Solution

  1. Step 1: Understand import effect on state

    The import command adds an existing resource to Terraform's state file without creating or deleting it.
  2. Step 2: Clarify what happens to the resource

    The resource remains unchanged in the cloud; Terraform just tracks it as aws_instance.web.
  3. Final Answer:

    Add the existing AWS instance i-1234567890abcdef0 to Terraform state under aws_instance.web -> Option C
  4. Quick Check:

    Import adds resource to state, no creation or deletion [OK]
Hint: Import updates state, not cloud resources [OK]
Common Mistakes:
  • Thinking import creates or deletes resources
  • Assuming import applies configuration changes
  • Confusing import with terraform apply
4. You ran terraform import aws_s3_bucket.example my-bucket but got an error: Resource aws_s3_bucket.example not found in configuration. What is the likely cause?
medium
A. The import command syntax is incorrect
B. The bucket my-bucket does not exist in AWS
C. Terraform version is outdated
D. The resource aws_s3_bucket.example is missing in Terraform files

Solution

  1. Step 1: Analyze error message

    The error says the resource address is not found in configuration, meaning Terraform files lack aws_s3_bucket.example.
  2. Step 2: Confirm resource presence

    Before import, Terraform must know the resource block to map the imported resource correctly.
  3. Final Answer:

    The resource aws_s3_bucket.example is missing in Terraform files -> Option D
  4. Quick Check:

    Resource must exist in config before import [OK]
Hint: Define resource in config before importing [OK]
Common Mistakes:
  • Ignoring missing resource block
  • Assuming import creates resource block
  • Blaming AWS resource existence first
5. You imported an existing AWS security group with terraform import aws_security_group.sg sg-0a1b2c3d4e5f6g7h8. After import, your Terraform configuration does not match the actual security group rules. What should you do next?
hard
A. Manually update your Terraform configuration to match the imported resource
B. Run terraform apply immediately to fix the resource
C. Delete the imported resource from state and re-import
D. Nothing, Terraform will auto-sync configuration

Solution

  1. Step 1: Understand import limitations

    Import adds the resource to state but does not update Terraform files to reflect actual settings.
  2. Step 2: Align configuration with reality

    You must manually edit Terraform configuration to match the imported resource's current rules to avoid unwanted changes on apply.
  3. Final Answer:

    Manually update your Terraform configuration to match the imported resource -> Option A
  4. Quick Check:

    Update config after import to match resource [OK]
Hint: Edit config to match imported resource before apply [OK]
Common Mistakes:
  • Expecting terraform apply to fix config automatically
  • Deleting and re-importing unnecessarily
  • Ignoring config mismatch risks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard