0
0
Terraformcloud~10 mins

Writing configuration for imported resources in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Writing configuration for imported resources
Identify existing resource
Import resource using CLI
Write matching Terraform config
Run terraform plan
Verify no changes needed
Manage resource with Terraform
This flow shows how to bring an existing resource under Terraform management by importing it, writing matching config, and verifying no changes.
Execution Sample
Terraform
terraform import aws_s3_bucket.mybucket my-existing-bucket

resource "aws_s3_bucket" "mybucket" {
  bucket = "my-existing-bucket"
  acl    = "private"
}
Import an existing S3 bucket and write Terraform config to match it.
Process Table
StepActionCLI/ConfigTerraform StateResult
1Identify existing resourceN/ANo resource in stateReady to import
2Import resourceterraform import aws_s3_bucket.mybucket my-existing-bucketState updated with resourceResource tracked by Terraform
3Write configresource "aws_s3_bucket" "mybucket" { bucket = "my-existing-bucket" acl = "private" }Config matches resourceTerraform knows resource details
4Run terraform planterraform planState and config comparedNo changes detected
5Manage resourceterraform applyState updated if neededResource managed by Terraform
6ExitN/AState and config syncedProcess complete
💡 No changes detected after plan, resource is fully managed
Status Tracker
VariableStartAfter ImportAfter Config WriteAfter PlanFinal
terraform_stateemptycontains aws_s3_bucket.mybucketunchangedunchangedunchanged
configurationemptyemptycontains aws_s3_bucket.mybucket configunchangedunchanged
Key Moments - 3 Insights
Why do I need to write the Terraform config after importing a resource?
Importing only adds the resource to Terraform state; without matching config, Terraform will try to delete or recreate it. See execution_table step 3 and 4.
What happens if the config does not match the imported resource?
Terraform plan will show changes to apply, possibly deleting or modifying the resource unexpectedly. See execution_table step 4.
Can I import multiple resources at once?
You must import each resource individually with its own import command and write matching config for each. See execution_table steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Terraform state after step 2?
AContains the imported resource
BEmpty, no resources tracked
CMatches the written config
DShows planned changes
💡 Hint
Check the 'Terraform State' column in row for step 2
At which step does Terraform detect no changes needed?
AStep 2 - Import resource
BStep 3 - Write config
CStep 4 - Run terraform plan
DStep 5 - Manage resource
💡 Hint
Look at the 'Result' column for step 4 in execution_table
If the config does not match the imported resource, what will terraform plan show?
ANo changes needed
BPlanned changes to update or replace resource
CErrors and stops
DResource removed from state
💡 Hint
Refer to key_moments about mismatched config and step 4 in execution_table
Concept Snapshot
Terraform import brings existing resources into state.
You must write matching config to avoid unwanted changes.
Run terraform plan to verify no changes.
Then manage resource normally with Terraform.
Full Transcript
To manage an existing cloud resource with Terraform, first identify the resource you want to import. Use the terraform import command to add it to Terraform's state file. This does not create configuration files, so you must write Terraform configuration that matches the imported resource's settings. After writing the config, run terraform plan to compare the state and config. If they match, no changes will be planned, meaning Terraform now fully manages the resource. This process prevents Terraform from trying to recreate or delete the resource. Always verify with terraform plan before applying changes.