In this blog, We will see an interesting tool that helps DevOps/SRE professionals working in the Azure Cloud.
Are you worried that your Infrastructure as Code (IAC) is not in a good state, and there have been lots of manual changes? Here is a solution provided by Azure - a tool named "Azure Export for Terraform (aztfexport)".
This tool assists in exporting the current Azure resources into Terraform code. Below, we will see the installation of this tool and how to use it.
Requirements:
1.A linux/Window machine
2.Terraform (>= v0.12)
3.az-cli
4.Azure subscription account
Step 1 : aztfexport installation,
This tool can be installed on all operating systems. Refer to the link below for installation instructions for other OS:
https://github.com/Azure/aztfexport
If you are installing it on macOS, open the terminal and execute the following command:
brew install aztfexport
Step 2 : Configure azure subscription
Execute below commands to configure the azure subscription in terminal,
az login or
az login --use-device-code
next set the subscription id,
az account set --subscription "subscription id"
Now that the Azure subscription is configured, let's proceed with trying out the tool.
In this subscription, I have a resource group named "devopsart-dev-rg" which contains a virtual machine (VM). We will generate the Terraform code for this VM.
Step 3 : Experiment "aztfexport" tool
Execute the below commands to generate the tf code,
Create a new directory in any name,
mkdir aztfexport && cd aztfexport
Below command will help to check the available option for this tool.
aztfexport --help
Execute the below command to generate the terraform code from "devopsart-dev-rg" rg
Syntax : aztfexport resource-group resource-grp-name
aztfexport resource-group devopsart-dev-rg
It will take few seconds to list the available resources in the given resource group(RG).
and it will list all the resources under the RG like below,
next enter "w" to import the resources and it will take some more time to generate it.
Once its completed, we can validate the tf files.
Step 4 : Validate the tf files
We will validate the generated files, and the following files are present in the directory,
main.tf,
provider.tf
terraform.tf
aztfexportResourceMapping.json
terraform.state (We can save this state file remotely by using below parameters)
aztfexport [subcommand] --backend-type=azurerm \
--backend-config=resource_group_name=<resource group name> \
--backend-config=storage_account_name=<account name> \
--backend-config=container_name=<container name> \
--backend-config=key=terraform.tfstate
Run, terraform plan
Nice!, it says there is no change is required in the Azure cloud infra.
Step 5 : Delete the azure resource and recreate with generated tf files,
The resources are deleted from Azure Portal under the dev rg,
Now run the terraform commands to create the resource,
cd aztfexport
terraform plan
Next execute,
terraform apply
Now all the resources are recreated with the generated tf files.
Thats all, We have installed aztfexport tool, generated tf files, Destroyed the azure resources and recreated with generated files.
check below link for the current limitations,
https://learn.microsoft.com/en-us/azure/developer/terraform/azure-export-for-terraform/export-terraform-concepts#limitations
References,
https://learn.microsoft.com/en-us/azure/developer/terraform/azure-export-for-terraform/export-terraform-overview
https://github.com/Azure/aztfexport
https://www.youtube.com/watch?v=LWk9SU7AmDA
https://learn.microsoft.com/en-us/azure/developer/terraform/azure-export-for-terraform/export-advanced-scenarios
Post a Comment