When working with Infrastructure as Code (IaC) it’s difficult to know sometimes where to start. You have a couple of options, go to the Terraform on Azure documentation, then figure out how to write some Terraform templates. Or you can start with a tool like NubesGen that allows you to build your Terraform files (and Bicep) from an easy set of options, spitting out a Terraform template that includes all of the best practices.
Those options are great when you’re wanting to create new infrastructure, but previously, you couldn’t import your existing infrastructure into Terraform. Ah, but now you can! Introducing Azure Terrafy!
Azure Terrafy allows you to easily and quickly import your existing Azure infrastructure into Terraform HCL AND import it into your Terraform state (Learn about state files here. This has been one of the BIGGEST and coolest feature releases that all of you in the community (and myself) have been asking for! It’s great to adopt new working tools and practices for when we start a new project, but every single customer I work with wants to know how to manage their legacy infrastructure. The biggest challenge we face in ITOps is managing that legacy infrastructure.
Let’s walk through how to get started using Azure Terrafy:
go install github.com/Azure/aztfy@latest
I opted for the latter option:
Note: You will also need to ensure that you have Azure CLI installed and have authenticated to Azure
Aztfy will now begin the import process:
It will show you the importing resources as it run, along with a progress bar:
Aztfy found 250 resources in my resource group along with their dependencies, scrolling through I can proceed to the end of the screen and review my options. From this view we can filter our results, show any errors/recommendations, or import our resources:
5. Type ‘w’ to import all of the viewed resources. Aztfy will begin to generate your Terraform scripts and it does all of the heavy lifting for you. Not only does it create your Terraform scripts, it will include the dependencies and update the state.
Once Azure Terrafy has completed, you can open your folder and see the newly generated template files:
You can open the Azure portal and visually check that the resource naming, dependencies and settings have been imported successfully.
6. The best way to validate the importing of your resources is by running ‘Terraform Plan’
This will confirm that your configuration matches your infrastructure. The output will confirm that no changes need to be made.
Congratulations! You’ve successfully imported your existing infrastructure in Azure into Terraform templates!
While this makes importing existing resources a breeze, there are some things to be conscious of at this point.
There is no such thing as a perfect tool, but the announcement of Azure Terrafy is a huge step in the right direction!
Read more about the aztfy announcement and the new AzAPI Terraform Provider (TL;DR: Day 0 support for ALL Azure resources when creating Terraform templates), the Microsoft team has done an amazing job at removing the need for ARM templates to be embedded in your Terraform templates along with launching this new tool. You can also check out the azty GitHub repo here for more details on how the tool works and new features.
Happy importing!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.