Its Day 97 of my 100 Days of Cloud journey and in todays post we’ll take a quick look at Azure Terrafy.
Azure Terrafy is a tool which you can use to bring your existing Azure resources into Terraform HCL and import it into Terraform state.
As we saw back on Day 36, Terraform state acts as a database to store information about what has been deployed in Azure. When running terraform plan, the tfstate file is refreshed to match what it can see in the environment. And if you use Terraform to manage resources, you should only use Terraform and not any other deployment tools as this can cause issues with your terraform configuration files and in turn issues with your Azure resources.
But if we’ve deployed infrastructure using means other than Terraform (such as ARM Templates, Bicep, PowerShell or manually using the Azure Portal), its difficult to keep those resources in a consistent state of configuration.
And thats where Azure Terrafy comes to the rescue!
So lets do a quick demo – on my local device I have the latest Terraform version installed and have also downloaded the Azure Terrafy binaries from GitHub. I also have Azure CLI installed and have authenticated to my subscription:
I’ve deployed a Resource Group in Azure and created a VM:
Let run aztfy.exe and see what options its giving us:
The main thing we see here is that we need to specify the resource group. So, we’ll run
C:\aztfy is where I’ve downloaded the Azure Terrafy binary file to, the location
C:\md-aztfy-rg that I’m running the command from is an empty directory where I want to store my terraform files once they get created.
Thats a good sign ….. so its initializing and is now interrogating the resource group to see what resources exist and if it can import them.
Once thats done, we get presented with this screen:
As we can see, the first line is the resource that has been identified, the second is the terraform provider that has been identified as being a match for the resource. As we can see, the majority have been identified except for one which is the virtual machine. If we scroll down using the controls listed at the bottom of the screen, there is an option to “show recommendation”.
For this one, its telling me no resource recommendation is available. Thats OK though, because we can hit enter and type in the correct resource:
Once thats done, we click enter to save that line, and then hit choose the option to import:
And as we can see thats started to import our configuration. And eventually we’ll get this screen:
And once thats finished we’ll see this:
So now lets open that directory from Visual Studio Code, and we’ll open the terraform.tfstate file:
Ok, so that looks great and everything looks to be good. But we need to test, so we’ll run
terraform plan to see if its worked:
And its telling me my infrastructure matches the configuration! So we can now manage the resources using Terraform!
Azure Terrafy is in the early stages of its development, but we can see that its a massive step forward for those who want to manage their existing resources using Terraform.
There are some great resources out there on Azure Terrafy:
- Blog Posts from April Edwards and Kyle Ruddy
- YouTube demo and run-through from Kevin Evans and Mike Pfeiffer
- The official Azure Terrafy Github page
Hope you enjoyed this post, until next time!