Manual import of Azure Resources to Terraform
Manual import of Azure Resources to Terraform Cloud
This approach is precise but time-consuming.
-
Ensure you have Azure CLI installed and working on your machine
-
Ensure you have current version of Terraform CLI installed and working locally
-
Change directory to your Terraform plan working directory
-
Assuming you are using remote state in Terraform cloud,
terraform login
-
Login to azure (
az login
) -
Select the correct subscription as active
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
-
Ensure your local Terraform plan has a correct
cloud
block to define the backend (docs) -
If there are essential variables defined in the cloud, replicate them in a local tfvars file in your working directory - remember to add this to
.gitignore
. -
For each resource to import, add the relevant statements to your Terraform plan
-
For each resource get the Azure Resource Id by running appropriate Azure CLI commands. The Resource Id is usually a long string starting
/subscriptions/...
-
For each resource import to the Terraform state:
terraform import -var-file="tfvars.tfvars" <resource name from terraform plan> <Azure Resource Id>
-
As a final test, push any changes to the plan, and watch the next run in Terraform Cloud - there should be no changes