Terraform State Import
Step-01: Introduction¶
- Terraform is able to import existing infrastructure.
- This allows you take resources you've created by some other means and bring it under Terraform management.
- This is a great way to slowly transition infrastructure to Terraform, or to be able to be confident that you can use Terraform in the future if it potentially doesn't support every feature you need today.
- Full Reference
- Azure Resource Group Terraform Import
Step-02: Create Azure Resource Group using Azure Mgmt Console¶
- Login to Azure Portal Management Console
- Go to -> Resource Groups -> Create
- Resource Group: myrg1
- Region: East US
- Click on Review + create
- Click on Create
Step-03: Create Basic Terraform Configuration¶
- c1-versions.tf
- c2-resource-group.tf
- Create a base Azure Resource Group resource
Step-04: Run Terraform Import to import Azure Resource Group to Terraform State¶
# Terraform Initialize
terraform init
# Terraform Import Command for Azure Resource Group
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example
terraform import azurerm_resource_group.example /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>
terraform import azurerm_resource_group.myrg /subscriptions/82808767-144c-4c66-a320-b30791668b0a/resourceGroups/myrg1
Observation:
1) terraform.tfstate file will be created locally in Terraform working directory
2) Review information about imported instance configuration in terraform.tfstate
# List Resources from Terraform State
terraform state list
Step-05: Start Building local c2-resource-group.tf¶
- By referring
terraform.tfstatefile and parallely runningterraform plancommand make changes to your terraform configurationc2-resource-group.tftill you get the messageNo changes. Infrastructure is up-to-dateforterraform planoutput
Step-06: Modify Resource Group from Terraform¶
- You have created this Azure Resource Group manually and now you made it as terraform managed
- Modify this resource group by adding new tags
Step-07: Execute Terraform Commands¶
# Terraform Plan
terraform plan
# Terraform Apply
terraform apply -auto-approve
Observation:
1) Azure Resource Group on Azure Cloud should have the recently added tags.
Step-08: Destroy all resources¶
- Destroy that using terraform
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
MARCH2026
Enroll Now on Udemy →
🎉 Offer