Bird
Raised Fist0
Terraformcloud~10 mins

Import state verification in Terraform - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Import state verification
Identify existing resource
↓
Run terraform import command
↓
Terraform maps resource to state
↓
Verify imported state matches resource
↓
Use terraform plan to confirm no changes
↓
Done
This flow shows how Terraform imports an existing resource into its state and verifies it matches the real resource.
Execution Sample
Terraform
terraform import aws_s3_bucket.mybucket my-existing-bucket
terraform plan
Imports an existing AWS S3 bucket into Terraform state and then checks for any differences.
Process Table
StepActionInput/CommandTerraform StateResult/Output
1Identify resourceExisting S3 bucket 'my-existing-bucket'State empty for aws_s3_bucket.mybucketReady to import
2Run importterraform import aws_s3_bucket.mybucket my-existing-bucketState updated with bucket infoResource mapped to state
3Verify stateterraform planState has aws_s3_bucket.mybucketNo changes detected, state matches resource
4End-State stableImport and verification complete
💡
Status Tracker
VariableStartAfter Step 2After Step 3Final
aws_s3_bucket.mybucketNot in stateImported with bucket attributesVerified matches resourceStable in state
Key Moments - 2 Insights
Why does terraform plan show no changes after import?
Because the imported state matches the actual resource configuration, terraform plan confirms no drift (see execution_table step 3).
What happens if the resource does not exist when running terraform import?
Terraform import will fail and state will not update, so no resource is mapped (refer to execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the Terraform state after running the import command?
AState updated with bucket info
BState is empty
CState shows errors
DState removed the resource
💡 Hint
Check execution_table row 2 under 'Terraform State'
At which step does terraform confirm that the imported resource matches the actual resource?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at execution_table row 3 under 'Result/Output'
If the resource did not exist, what would happen to the state after step 2?
AState would be updated with partial info
BState would remain empty
CState would contain errors but resource added
DState would remove all resources
💡 Hint
Refer to key_moments about import failure and execution_table step 2
Concept Snapshot
terraform import <resource> <id>
Maps existing resource to Terraform state.
Run terraform plan to verify no changes.
If plan shows no changes, import succeeded.
If resource missing, import fails with error.
Full Transcript
Import state verification in Terraform involves identifying an existing resource, running the terraform import command to map it into Terraform's state file, and then verifying the import by running terraform plan. The plan should show no changes if the state matches the actual resource. If the resource does not exist, the import command fails and the state remains unchanged. This process ensures Terraform can manage existing infrastructure without recreating it.

Practice

(1/5)
1. What is the main purpose of terraform import in Terraform?
easy
A. To create new resources in the cloud
B. To update Terraform version
C. To delete resources from the cloud
D. To bring existing cloud resources into Terraform state management

Solution

  1. Step 1: Understand Terraform state

    Terraform state tracks resources it manages. Existing resources outside Terraform are unknown to it.
  2. Step 2: Role of terraform import

    The terraform import command adds existing resources to Terraform state without creating or deleting them.
  3. Final Answer:

    To bring existing cloud resources into Terraform state management -> Option D
  4. Quick Check:

    Import = Add existing resource to state [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 EC2 instance with ID i-1234567890abcdef0 into Terraform resource aws_instance.example?
easy
A. terraform import aws_instance i-1234567890abcdef0.example
B. terraform import aws_instance.example i-1234567890abcdef0
C. terraform import i-1234567890abcdef0 aws_instance.example
D. terraform import example.aws_instance i-1234567890abcdef0

Solution

  1. Step 1: Identify correct command order

    The syntax is terraform import <resource_address> <resource_id>.
  2. Step 2: Match resource address and ID

    Resource address is aws_instance.example, ID is i-1234567890abcdef0. So terraform import aws_instance.example i-1234567890abcdef0 matches syntax correctly.
  3. Final Answer:

    terraform import aws_instance.example i-1234567890abcdef0 -> Option B
  4. Quick Check:

    Import syntax = terraform import resource_address resource_id [OK]
Hint: Resource address first, then resource ID in import command [OK]
Common Mistakes:
  • Swapping resource address and ID
  • Using dot in wrong place
  • Missing resource type or name
3. After running terraform import aws_s3_bucket.mybucket my-bucket-name, what will terraform plan show if the Terraform configuration matches the imported bucket exactly?
medium
A. Plan to delete the imported bucket.
B. Plan to create a new S3 bucket.
C. No changes. Terraform state matches configuration.
D. Error: Resource not found.

Solution

  1. Step 1: Understand import effect

    Import adds existing resource to state but does not change configuration files.
  2. Step 2: Compare state and config

    If configuration matches the imported resource exactly, terraform plan shows no changes needed.
  3. Final Answer:

    No changes. Terraform state matches configuration. -> Option C
  4. Quick Check:

    Matching config and state = no changes [OK]
Hint: Plan shows no changes if config matches imported state [OK]
Common Mistakes:
  • Expecting terraform plan to create or delete after import
  • Confusing import with apply
  • Ignoring configuration mismatch
4. You ran terraform import aws_db_instance.db1 db-1234 but terraform plan shows the instance will be destroyed and recreated. What is the most likely cause?
medium
A. The Terraform configuration does not match the imported resource attributes
B. The import command was run with wrong resource ID
C. Terraform state file is corrupted
D. Terraform version is outdated

Solution

  1. Step 1: Analyze import and plan mismatch

    If plan wants to destroy and recreate, it means config differs from imported resource state.
  2. Step 2: Confirm configuration matches resource

    Mismatch in attributes causes Terraform to think resource needs replacement.
  3. Final Answer:

    The Terraform configuration does not match the imported resource attributes -> Option A
  4. Quick Check:

    Config mismatch causes resource replacement plan [OK]
Hint: Mismatch between config and imported resource causes replacement plan [OK]
Common Mistakes:
  • Assuming import ID error always causes plan changes
  • Blaming Terraform version without checking config
  • Ignoring attribute differences
5. You want to import multiple existing AWS security groups into Terraform. Which approach ensures correct import and verification?
hard
A. Run terraform import for each security group, then run terraform plan to verify state matches configuration
B. Run terraform apply directly to create new security groups
C. Delete existing security groups and recreate with Terraform
D. Edit the Terraform state file manually to add security groups

Solution

  1. Step 1: Import each existing security group

    Use terraform import with correct resource address and IDs for each group.
  2. Step 2: Verify with terraform plan

    Run terraform plan to check if imported state matches configuration and no unexpected changes occur.
  3. Final Answer:

    Run terraform import for each security group, then run terraform plan to verify state matches configuration -> Option A
  4. Quick Check:

    Import each resource, then verify with plan [OK]
Hint: Import individually and verify with plan to avoid surprises [OK]
Common Mistakes:
  • Skipping plan after import
  • Trying to create duplicates with apply
  • Manually editing state file causing errors
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