A tool to bring your existing Azure resources under the management of Terraform.
Azure Terrafy imports the resources inside a resource group, which are supported by the Terraform provider, into the Terraform state, and generates the valid Terraform configuration. Both the Terraform state and configuration should be consistent with the resources' remote state, i.e.,
terraform plan shows no diff. The user then is able to use Terraform to manage these resources.
Precompiled binaries for Windows, OS X, Linux are available at Releases.
Note: The release is in the format of
.tar.gz, Windows users might want to have 7zip installed to extract the files.
From Go toolchain
go install github.com/Azure/[email protected]
Then you can go ahead and run
aztfy [option] <resource group name> -o string Specify output dir. Default is a dir under the user cache dir, which is named after the resource group name -v Print version
The tool will then list all the resources resides in the specified resource group.
For each resource,
aztfy will ask the user to input the Terraform resource type and name for each Azure resource in the form of
<resource type>.<resource name> (e.g.
azurerm_linux_virtual_machine.example). Users can press
r to see the possible resource type for the selected import item, though this is not guaranteed to be 100% accurate.
In some cases, there are Azure resources that have no corresponding Terraform resource (e.g. due to lacks of Terraform support), or some resource might be created as a side effect of provisioning another resource (e.g. the Disk resource is created automatically when provisioning a VM). In these cases, you can skip these resources without typing anything.
After getting the input from user,
aztfy will run
terraform import under the hood to import each resource. Then it will run
terraform add -from-state to generate the Terraform template for each imported resource. Whereas there are kinds of limitations causing the output of
terraform add to be an invalid Terraform template in most cases.
aztfy will leverage extra knowledge from the provider (which is generated from the provider codebase) to further manipulate the template, to make it pass the Terraform validations against the provider.
As the last step,
aztfy will leverage the ARM template to inject dependencies between each resource. This makes the generated Terraform template to be useful.
Some Azure resources are modeled differently in AzureRM provider, which means there might be N:M mapping between the Azure resources and the Terraform resources.
For example, the
azurerm_lb_backend_address_pool_address is actually a property of
azurerm_lb_backend_address_pool, whilst in the AzureRM provider, it has its own resource and a synthetic resource ID as
Another popular case is that in the AzureRM provider, there are a bunch of "association" resources, e.g. the
azurerm_network_interface_security_group_association. These "association" resources represent the association relationship between two Terraform resources (in this case they are
azurerm_network_security_group). They also have some synthetic resource ID, e.g.
Currently, this tool only works on the assumption that there is 1:1 mapping between Azure resources and the Terraform resources.
How to develop with vscode
vs pre requiries
Install Go extension from "Go Team at Google"
Install dependencies when ask in the editor.
Build without optimision
go build -gcflags=all="-N -l" (To run, in context of the folder)
Add some code in the main.go to stop the init.
var reader = bufio.NewReader(os.Stdin) os.Setenv("AZTFY_DEBUG", "true") os.Setenv("AZTFY_MOCK_CLIENT", "false") os.Setenv("AZTFY_LOGFILE", "aztfylogs.log") log.Println("Main hitted") input, _ := reader.ReadString('\n') value := strings.TrimSpace(input) fmt.Printf("input: %v", value)
- Run app
Get pid of the app
- Linux : pgrep aztfy
- Windows : Task manager / tab detail
Update launch setting processId with pid (Sample in folder .vscode\launch.json)
launch debug session