Importing Azure Resource using Terrafy
Date published:
This post will show how to import Azure Resources using Terrafy. When you are working with Infrastructure as Code (IaC) and do not know where to start except going on the Terraform or Azure Documentation, try to write terraform code. If you deployed resources using other methods like ARM Template, Bicep, Powershell/CLI, or manually, keeping track of those configuration states can be difficult.
Azure Terrafy allows you to quickly import your existing Azure Resources into Terraform HCL and import them into the Terraform state file. This is an excellent feature everyone has been asking for for a while. In this tutorial, I will deploy a windows VM and later delete the state file and use the import feature to recover it.
Before you start, you need the following:
- Define your infrastructure using Terraform.
- Run import to update your state file.
- Run a plan to verify the import is successful.
Installation
1) go install github.com/Azure/aztfy@latest. Click here to download it https://github.com/azure/aztfy
2) Once you have it installed, you can verify it by typing running aztfy on your terminal
3) Deploying your Infrastructure
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "rg" {
name = "example-resources"
location = "West Europe"
}
resource "azurerm_storage_account" "storage_account" {
name = "linuxfunctionappsa"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_service_plan" "app_service" {
name = "example-app-service-plan"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
os_type = "Linux"
sku_name = "Y1"
}
resource "azurerm_linux_function_app" "function" {
name = "example-linux-function-app"
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
storage_account_name = azurerm_storage_account.storage_account.name
service_plan_id = azurerm_service_plan.app_service.id
site_config {}
}
Go to your terraform directory and run a terraform init, and once initialised, run a terraform plan.
After you have planned the Infrastructure you want to deploy, just run a terraform apply to start deploying your resource to Azure.
Once deployed to Azure, I will delete the state file created locally, and you will see how easy it is to recover it again.
3) View your current state list
Afterwards, view your state list, and you should be able to see six resources inside.
Remove local state file
Run the command : rm -rf ./.terraform/terraform.tfstate
***3) Importing Azure Resources ***
In this stage, I will use Terrafy import to import the resources from Azure and re-create your deleted state file. Create a directory where you want to store your state file and run the aztfy command followed by your resource group name:
Run aztify rg vm-resources
After running the command, this will look into the specified resource group and find any exciting resources. By scrolling through, I can proceed to the end of the screen and review your option, filter your results, show any errors/recommendations, or import our resources.
Once it has started importing, I will need to wait for several minutes for it to complete
This result shows the newly created state file and the results of the state when running a terraform plan. By running the plan, it shows that there are no changes made to the infrastructure.
I think the new feature is very cool as it can help use cases when configuring a backup; however, I find it is having an issue with complex modules. I cannot wait to see this feature improve in the future.
Further Reading