Bird
Raised Fist0
Terraformcloud~10 mins

Import block syntax (Terraform 1.5+) - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Import block syntax (Terraform 1.5+)
Write import block
↓
Run terraform apply
↓
Terraform reads import block
↓
Terraform imports resource state
↓
Resource state linked to config
↓
Terraform plan shows no changes
↓
Done
The import block tells Terraform to link existing resources to the config during apply, so state is updated without manual CLI import.
Execution Sample
Terraform
import {
  id = "resource-id"
  to = aws_s3_bucket.example
}
This import block links an existing AWS S3 bucket with id 'resource-id' to the Terraform resource aws_s3_bucket.example.
Process Table
StepActionInputTerraform BehaviorResult
1Read import blockimport { id = "resource-id" to = aws_s3_bucket.example }Terraform parses import blockImport info stored for apply
2terraform apply startsTerraform prepares to import resourceReady to import resource state
3Import resource stateid = resource-idTerraform fetches existing resource stateState linked to aws_s3_bucket.example
4Update state fileTerraform updates local state with imported resourceState file now includes resource
5Plan after importTerraform compares config and stateNo changes detected
6Apply completesTerraform finishes without creating or destroyingResource managed by Terraform now
7ExitNo more stepsImport process completeTerraform state and config synced
💡 Import block processed, resource state linked, no further changes needed.
Status Tracker
VariableStartAfter Step 3After Step 4Final
import_blockNot readParsed with id and toUsed to fetch resource stateProcessed and cleared
terraform_stateEmpty for resourceResource state fetchedState file updatedState synced with config
Key Moments - 3 Insights
Why doesn't Terraform create a new resource when using the import block?
Because the import block tells Terraform to link an existing resource by its ID, so Terraform updates the state instead of creating a new resource. See execution_table step 3 and 4.
What happens if the resource ID in the import block is incorrect?
Terraform will fail to fetch the resource state during apply (step 3), causing an error and stopping the import process.
Does the import block change the Terraform configuration files?
No, the import block only affects state linking during apply. The configuration remains the same. This is shown in step 5 where plan shows no changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does Terraform link the existing resource state to the configuration?
AStep 2
BStep 3
CStep 5
DStep 6
💡 Hint
Check the 'Terraform Behavior' column for when resource state is fetched and linked.
According to variable_tracker, what is the state of terraform_state after step 4?
AEmpty for resource
BResource state fetched
CState file updated
DState synced with config
💡 Hint
Look at the 'terraform_state' row and the 'After Step 4' column.
If the import block is missing the 'to' attribute, what would happen during execution?
ATerraform would fail to parse the import block
BTerraform would import the resource anyway
CTerraform would create a new resource
DTerraform would skip the import silently
💡 Hint
Refer to execution_table step 1 where Terraform parses the import block.
Concept Snapshot
Import block syntax (Terraform 1.5+):
import {
  id = "resource-id"
  to = resource_type.resource_name
}
Use in config to link existing resources during terraform apply.
No manual CLI import needed.
Terraform updates state to manage resource without recreating.
Full Transcript
The import block in Terraform 1.5+ allows you to link existing cloud resources to your Terraform configuration automatically during terraform apply. You write an import block specifying the resource ID and the resource address in your config. When you run terraform apply, Terraform reads this block, fetches the existing resource state, and updates the local state file. This means Terraform now manages the resource without creating or destroying it. The plan after import shows no changes, confirming the resource is linked. If the import block is incorrect or missing required fields, Terraform will fail during parsing or import. This feature simplifies managing existing infrastructure by avoiding manual CLI import commands.

Practice

(1/5)
1. What is the main purpose of the import block in Terraform 1.5+?
easy
A. To create new resources automatically
B. To delete resources from the cloud provider
C. To let Terraform track existing resources without recreating them
D. To backup Terraform state files

Solution

  1. Step 1: Understand the import block purpose

    The import block is designed to connect Terraform state with existing cloud resources.
  2. Step 2: Differentiate from other actions

    It does not create or delete resources but tracks them to avoid recreation.
  3. Final Answer:

    To let Terraform track existing resources without recreating them -> Option C
  4. Quick Check:

    Import block = track existing resources [OK]
Hint: Import block links Terraform to existing resources [OK]
Common Mistakes:
  • Thinking import creates new resources
  • Confusing import with resource deletion
  • Assuming import backs up state files
2. Which of the following is the correct syntax for an import block in Terraform 1.5+?
easy
A. import { target = aws_instance.example value = "i-1234567890abcdef0" }
B. import { to = aws_instance.example id = "i-1234567890abcdef0" }
C. import { resource = aws_instance.example key = "i-1234567890abcdef0" }
D. import { name = aws_instance.example identifier = "i-1234567890abcdef0" }

Solution

  1. Step 1: Recall import block keys

    The import block uses to for the Terraform resource and id for the cloud resource ID.
  2. Step 2: Match syntax with options

    Only import { to = aws_instance.example id = "i-1234567890abcdef0" } uses to and id correctly.
  3. Final Answer:

    import { to = aws_instance.example id = "i-1234567890abcdef0" } -> Option B
  4. Quick Check:

    Import keys = to and id [OK]
Hint: Remember keys: to = resource, id = cloud ID [OK]
Common Mistakes:
  • Using wrong keys like resource or target
  • Confusing id with identifier
  • Missing quotes around the id value
3. Given this import block:
import {
  to = aws_s3_bucket.mybucket
  id = "my-existing-bucket"
}
What will Terraform do after applying this import block?
medium
A. Track the existing S3 bucket 'my-existing-bucket' without creating it
B. Create a new S3 bucket named 'my-existing-bucket'
C. Delete the S3 bucket 'my-existing-bucket'
D. Throw a syntax error due to missing resource configuration

Solution

  1. Step 1: Understand import block effect

    The import block links Terraform state to an existing resource by its ID without creating or deleting it.
  2. Step 2: Confirm behavior after import

    Terraform will track the existing bucket named 'my-existing-bucket' safely.
  3. Final Answer:

    Track the existing S3 bucket 'my-existing-bucket' without creating it -> Option A
  4. Quick Check:

    Import block = track existing resource [OK]
Hint: Import links state to existing resource, no creation [OK]
Common Mistakes:
  • Assuming import creates new resources
  • Thinking import deletes resources
  • Confusing import with resource configuration
4. You wrote this import block:
import {
  to = aws_instance.web
  id = i-0abcd1234efgh5678
}
Terraform gives an error when applying. What is the likely cause?
medium
A. Terraform 1.5+ does not support import blocks
B. The to value should be the cloud resource ID
C. The import block must be inside a resource block
D. The id value is missing quotes

Solution

  1. Step 1: Check syntax for id value

    The id value must be a string, so it requires quotes around it.
  2. Step 2: Identify error cause

    Missing quotes around i-0abcd1234efgh5678 causes a syntax error.
  3. Final Answer:

    The id value is missing quotes -> Option D
  4. Quick Check:

    id value must be quoted string [OK]
Hint: Always quote id values in import blocks [OK]
Common Mistakes:
  • Not quoting string IDs
  • Confusing to and id values
  • Placing import block inside resource block
5. You want to import an existing Azure resource group with ID "/subscriptions/123/resourceGroups/mygroup" into Terraform resource azurerm_resource_group.main. Which import block is correct and follows best practices?
hard
A. import { to = azurerm_resource_group.main id = "/subscriptions/123/resourceGroups/mygroup" } resource "azurerm_resource_group" "main" { name = "mygroup" location = "eastus" }
B. import { to = azurerm_resource_group.main id = "mygroup" } resource "azurerm_resource_group" "main" { name = "mygroup" location = "eastus" }
C. import { to = azurerm_resource_group.main id = "/subscriptions/123/resourceGroups/mygroup" } resource "azurerm_resource_group" "main" { location = "eastus" }
D. import { to = azurerm_resource_group.main id = "mygroup" } resource "azurerm_resource_group" "main" { location = "eastus" }

Solution

  1. Step 1: Use full resource ID in import block

    The id must be the full Azure resource ID string to correctly identify the resource.
  2. Step 2: Provide matching resource configuration

    After import, the resource block must include all required attributes like name and location to manage the resource safely.
  3. Final Answer:

    Import block with full ID and resource block with name and location -> Option A
  4. Quick Check:

    Full ID + matching config = correct import [OK]
Hint: Use full resource ID and matching config after import [OK]
Common Mistakes:
  • Using partial ID instead of full resource ID
  • Omitting required resource attributes after import
  • Assuming import block replaces resource configuration
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