0
0
Terraformcloud~10 mins

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

Choose your learning style9 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.