My Digital Garden

Manual import of Azure Resources to Terraform

Manual import of Azure Resources to Terraform Cloud

This approach is precise but time-consuming.

  1. Ensure you have azure-cli installed and working on your machine

  2. Ensure you have current version of terraform-cli installed and working locally

  3. Change directory to your Terraform plan working directory

  4. Assuming you are using remote state in Terraform cloud, terraform login

  5. Login to azure (az login)

  6. Select the correct subscription as active

    az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

  7. Ensure your local Terraform plan has a correct cloud block to define the backend (docs)

  8. 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.

  9. For each resource to import, add the relevant statements to your Terraform plan

  10. For each resource get the Azure Resource Id by running appropriate Azure CLI commands. The Resource Id is usally a long string starting /subscriptions/...

  11. For each resource import to the Terraform state:

    terraform import -var-file="tfvars.tfvars" <resource name from terraform plan> <Azure Resource Id>

  12. As a final test, push any changes to the plan, and watch the next run in Terraform Cloud - there should be no changes

See also