Christian Correa Monsalve - Cloud Center of Excellence
While infrastructure as code (IAC) has seamlessly integrated with cloud services, agile practices, and DevOps methodologies over the years, not all organizations worldwide have embraced this practice. Some may have adopted it later, post their initial adoption of cloud technologies. Enter Azure Terrafy, a tool offered by Azure that simplifies the process. With just a single command, it enables users to export resources into Terraform code along with their dependencies. This tool empowers users to export resources from a specific subscription or filter by resource types according to their preferences.
How it works
The tool to be used is called aztfexport and can be downloaded from the following link: https://github.com/Azure/aztfexport
Once installed, running it is simple; just run the aztfexport command and the name of the resource group you want to export.
The tool will query through Azure Graph the available resources in the resource group, and you will be able to choose from the list of resources found which ones to export and which ones not to export, just select the resource you do not want to export and delete it.
Once you are sure of what you want to export, you only need to press the "w" key.
If an error is encountered in the import, the tool will pause the process and indicate which resources have failed, by pressing the "e" key you can identify the error, resolve it and continue with the import.
Improving the outputs
At the end of the process, the tool will save the export to a .tf file and create a Terraform state, so if you run a Terraform plan with that content, Terraform will not identify any pending changes to apply, the resource group infrastructure is now in code, and you can apply new changes to it.
The code exported by the tool is useful to continue managing the same project, however, the advantage of IAC is that the code can be reused either in other projects or by rebuilding the same infrastructure in another environment. The recommendations on how to achieve this are as follows.
- Create a variable file with the configurations that can be considered different in each deployment and rename the exported resources referencing the variables created.
- Identify references to other resources and modify the static values by the outputs of the related resource.
- Remove keys and secrets, such as key vault secrets from the code and reference them as a secure variable.
Test your new code
To ensure that the exported code is functional to be deployed in the new environment, modify the corresponding variables, and run a terraform plan, if any errors are found, it will be easy to identify the cause thanks to the information provided by terraform.
At the end, you will be able to deploy all your resources without any inconvenience and start managing your new terraform code. Welcome to IAC.
With our TP expertise, we can move infrastructure that was manually deployed in Azure to Infrastructure as Code (IaC) and take advantage of benefits such as code reuse to accelerate future deployments.
TP is a Microsoft Azure Solutions Partner for Data & AI, highlighting our expertise in creating tailored analytics and AI solutions. We help businesses tackle challenges, boost efficiency, and gain valuable insights.
Visit our technology services page to learn more.