📖 Scenario: You are managing cloud infrastructure using Terraform. Some resources like virtual machines or storage buckets were created manually or by other tools. To manage them properly with Terraform, you need to import these existing resources into your Terraform state.This helps keep your infrastructure organized and prevents accidental resource duplication or deletion.
🎯 Goal: Learn how to define a Terraform resource and import an existing cloud resource into Terraform state to manage it safely.
📋 What You'll Learn
Create a Terraform resource block for an existing cloud resource
Add a variable to hold the resource identifier
Use the Terraform import command to link the existing resource
Verify the resource is managed by Terraform without recreating it
💡 Why This Matters
🌍 Real World
Many cloud resources are created outside Terraform. Importing them lets you manage all infrastructure consistently in one place.
💼 Career
Cloud engineers and DevOps professionals often import existing resources to maintain infrastructure as code and avoid manual errors.
Progress0 / 4 steps
1
Define a Terraform resource block for an existing AWS S3 bucket
Create a Terraform resource block named aws_s3_bucket with the resource name existing_bucket. Set the bucket attribute to my-existing-bucket exactly.
Terraform
Hint
Use the resource keyword, specify the resource type aws_s3_bucket, and name it existing_bucket. Set the bucket attribute to the exact bucket name.
2
Add a variable to hold the S3 bucket name
Add a Terraform variable named bucket_name with a default value of my-existing-bucket.
Terraform
Hint
Define a variable block with the name bucket_name and set its default to the bucket name. Use var.bucket_name in the resource block.
3
Import the existing S3 bucket into Terraform state
Use the Terraform import command to import the existing S3 bucket named my-existing-bucket into the resource aws_s3_bucket.existing_bucket. Write the exact command as: terraform import aws_s3_bucket.existing_bucket my-existing-bucket.
Terraform
Hint
Use the terraform import command with the resource address and the existing bucket name.
4
Verify the imported resource is managed by Terraform
Add the lifecycle block inside the aws_s3_bucket.existing_bucket resource with prevent_destroy = true to avoid accidental deletion.
Terraform
Hint
Inside the resource block, add a lifecycle block with prevent_destroy = true to protect the resource.
Practice
(1/5)
1. Why is it important to import existing resources into Terraform before managing them?
easy
A. To automatically delete unused resources in the cloud
B. To avoid creating duplicate resources and keep infrastructure organized
C. To speed up Terraform plan execution by skipping resource checks
D. To convert resources into a different cloud provider format
Solution
Step 1: Understand Terraform resource management
Terraform manages resources by tracking them in its state file. If a resource exists but is not in the state, Terraform may try to create it again.
Step 2: Role of importing existing resources
Importing adds existing resources to the state, preventing duplication and keeping infrastructure consistent.
Final Answer:
To avoid creating duplicate resources and keep infrastructure organized -> Option B
Quick Check:
Importing prevents duplicates [OK]
Hint: Import existing resources to avoid duplicates and conflicts [OK]
Common Mistakes:
Thinking import speeds up Terraform plan
Believing import deletes resources
Assuming import changes cloud provider
2. Which is the correct syntax to import an existing 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 my-bucket
D. terraform import aws_s3_bucket.example bucket=my-bucket
The command format is terraform import [resource_address] [resource_id]. The resource address comes first, then the ID.
Step 2: Apply to the example
Here, resource address is aws_s3_bucket.example and ID is my-bucket. So the correct command is terraform import aws_s3_bucket.example my-bucket.
Final Answer:
terraform import aws_s3_bucket.example my-bucket -> Option C
Quick Check:
Command format = resource_address then ID [OK]
Hint: Remember: import command is 'terraform import resource_address resource_id' [OK]
Common Mistakes:
Swapping resource address and ID order
Adding extra keywords like 'bucket='
Separating resource address incorrectly
3. Given this Terraform import command: terraform import aws_instance.web i-1234567890abcdef0 What will Terraform do after this import?
medium
A. Add the EC2 instance with ID i-1234567890abcdef0 to the Terraform state under aws_instance.web
B. Create a new EC2 instance named aws_instance.web with ID i-1234567890abcdef0
C. Delete the EC2 instance i-1234567890abcdef0 from AWS
D. Export the EC2 instance configuration to a file named aws_instance.web
Solution
Step 1: Understand what terraform import does
The import command adds an existing resource to Terraform's state file, linking the resource address to the real resource ID.
Step 2: Apply to the EC2 instance example
Here, the EC2 instance with ID i-1234567890abcdef0 is linked to the Terraform resource aws_instance.web. No new resource is created or deleted.
Final Answer:
Add the EC2 instance with ID i-1234567890abcdef0 to the Terraform state under aws_instance.web -> Option A
Quick Check:
Import adds resource to state, no creation or deletion [OK]
Hint: Import links existing resource to Terraform state without changes [OK]
Common Mistakes:
Thinking import creates or deletes resources
Confusing import with export or backup
Assuming import modifies resource configuration
4. You ran terraform import aws_s3_bucket.example my-bucket but Terraform shows an error: Resource aws_s3_bucket.example not found in configuration. What is the likely cause?
medium
A. The resource aws_s3_bucket.example is not defined in your Terraform files
B. The bucket my-bucket does not exist in AWS
C. You used the wrong AWS region in your provider configuration
D. Terraform import command requires a plan before importing
Solution
Step 1: Analyze the error message
The error says the resource address aws_s3_bucket.example is not found in the Terraform configuration files.
Step 2: Understand import requirements
Terraform import requires the resource block to be defined in the configuration before importing. Without it, Terraform cannot map the import.
Final Answer:
The resource aws_s3_bucket.example is not defined in your Terraform files -> Option A
Quick Check:
Resource must exist in config before import [OK]
Hint: Define resource in config before importing it [OK]
Blaming AWS bucket existence without checking config
Thinking import requires terraform plan first
5. You have an existing Azure virtual network created outside Terraform. You want to manage it with Terraform without recreating it. Which steps should you follow?
hard
A. Use terraform state rm to remove the network from state before importing
B. Run terraform apply immediately to let Terraform detect and manage the network
C. Delete the existing network and create it again with Terraform configuration
D. Write the Terraform resource block matching the existing network, then run terraform import with the resource address and network ID
Solution
Step 1: Define the resource in Terraform configuration
Write a resource block that matches the existing Azure virtual network's settings so Terraform knows what to manage.
Step 2: Import the existing network into Terraform state
Use terraform import with the resource address and the actual network ID to link the existing resource to Terraform.
Final Answer:
Write the Terraform resource block matching the existing network, then run terraform import with the resource address and network ID -> Option D
Quick Check:
Define resource then import existing resource [OK]
Hint: Define resource first, then import existing infrastructure [OK]
Common Mistakes:
Running apply without import causes resource recreation
Deleting existing resource unnecessarily
Removing state before import causes loss of tracking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.