Skip to content

Commit

Permalink
remove some too-detailed steps (as that might be different per indivi…
Browse files Browse the repository at this point in the history
…dual)
  • Loading branch information
magodo committed Oct 20, 2021
1 parent 4b6692e commit a443292
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,49 @@

A tool to bring your existing Azure resources under the management of Terraform.


## Goal

* Import all the AzureRM provider supported resources inside the resource group that the user specifies into to Terraform state
* Generate a valid Terraform configuration
* After running this tool, the Terraform state and configuration should be consistent with the resources' remote state, i.e., `terraform plan` shows no diff. The user then can use Terraform to manage these resources.
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.

## Install

### From Release

## Prerequisites
For Windows Devices:
* 7zip to open the installer
* Install the Azure CLI
* [Install Terraform and set the path]( https://docs.microsoft.com/en-us/azure/developer/terraform/get-started-windows-powershell?tabs=azure-powershell)
* Create a service principal
Precompiled binaries for Windows, OS X, Linux are available at [Releases](https://github.com/magodo/aztfy/releases).

Note: The release is in the format of `.tar.gz`, Windows users might want to have [7zip](https://www.7-zip.org/download.html) installed to extract the files.

## Install
* Precompiled binaries for Windows, OS X, Linux are available at [Releases](https://github.com/magodo/aztfy/releases).
* Windows: Download the latest binary, extract the files using 7zip
### From Go toolchain

* Linux:
```bash
go install github.com/magodo/aztfy@latest
```


## Usage

Follow the [authentication guide](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs#authenticating-to-azure) from the Terraform AzureRM provider to authenticate to Azure. The simplist way is to [install and login via the Azure CLI](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/azure_cli).

Then you can go ahead and run:

```shell
$ az group list --output table
$ aztfy <resource_group_name>
```

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`).

Press `enter` on each line to edit or skip the resource.
The tool will then list all the resources resides in the specified resource group.

NB: In some cases, there are 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 directly press enter without typing anything to skip that resource.
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`).

After getting the input from user, `aztfy` will run `terraform import` 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](https://github.com/apparentlymart/terrafy/blob/main/docs/quirks.md) 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.
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.

As a last step, `aztfy` will leverage the ARM template to inject dependencies between each resource. This makes the generated Terraform template to be useful.
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](https://github.com/apparentlymart/terrafy/blob/main/docs/quirks.md) 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.

## To Test
To test run terraform plan on the main.tf file that has been created by aztfy
The file is created in %AppData%\Local\aztfy\<resource group name>
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.

## Demo

[![asciicast](https://asciinema.org/a/iPTGS6E2CSxpYPtbPQhWmxLdu.svg)](https://asciinema.org/a/iPTGS6E2CSxpYPtbPQhWmxLdu)

## Limitation
When resources have a long name, they are cut off in the aztfy UI and will need to be skipped and manually added in later.[issue17]( https://github.com/magodo/aztfy/issues/17)

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.

Expand All @@ -67,5 +53,3 @@ For example, the `azurerm_lb_backend_address_pool_address` is actually a propert
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_interface` and `azurerm_network_security_group`). They also have some synthetic resource ID, e.g. `/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/mygroup1/providers/microsoft.network/networkInterfaces/example|/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.Network/networkSecurityGroups/group1`.

Currently, this tool only works on the assumption that there is 1:1 mapping between Azure resources and the Terraform resources.


2 comments on commit a443292

@simone-bennett
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I documented all of the things that tripped me up so it would be good to have that info somewhere, maybe in a windows section at the bottom? Up to you tho.

@magodo
Copy link
Collaborator Author

@magodo magodo commented on a443292 Oct 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simone-bennett I understand. I recall you have two major problems:

  1. How to install and use the tool from CLI? The install part is now complemented by mentioning the file format of the pre-built release, which should be enough for users to further figure out how to decompress the release. About how to use a tool from CLI on windows, that is a general topic and should be easy to "google" the solution, which means we don't have to mention that step here so as to make the README more focused.
  2. How to authenticate in Azure? The terraform provider document here is indeed a step-by-step guide. On the other hand, most users of this tool is Azure Terraform provider user, which means they should already has that knowledge.

Based on above two reasons, I did some trims on the documentation here. WDYT?

Please sign in to comment.