Introduction to IaC
Infrastructure as a code (IaC) is the management and deployment of the cloud resources such as (networks, virtual machines, load balances, etc) in a descriptive model, we can treat IaC as DevOps practices such as versioning the source code.
IaC evolved to solve the problem of environmental drift. Without IaC development teams has to maintain a separate environment for each environment. With time this may change the configurations of the environments. To avoid such scenarios we can use IaC check this link for more about IaC.
Terraformer
Terraformar is an open-source tool used to generate terraform from the existing infrastructure. If you deploying a fresh new infrastructure to the cloud it’s easy to move to terraform. But consider you already have an existing environment and you need to use IaC (terraform) to manage existing infrastructure.
Terraformer is a tool we can use to generate the existing environment in to terraform code. This tool helps to generate multiple cloud platforms and generate different types of cloud resources. We can follow the link attached above to the tool to see updated platform support and resources supported for each platform.
Lets look in to how this can be done in action
This demo is based on the Azure cloud and the steps may differ from other platforms. Refer to the Readme.md file for information.
Authenticate with Azure
We can authenticate to Azure using several methods, such as Azure CLI, Service Principle Name
Follow this doc to know more about creating a Service Principle Name. Ten export the following environment variable to the terminal.
# Service Principal with Client Secret
export ARM_SUBSCRIPTION_ID=[SUBSCRIPTION_ID]
export ARM_CLIENT_ID=[CLIENT_ID]
export ARM_CLIENT_SECRET=[CLIENT_SECRET]
export ARM_TENANT_ID=[TENANT_ID]
Next, we need to create .tf file with the provider to download the Azure provider, later we use a downloaded provider to generate terraform existing resources. I saved the file as init.tf
provider "azurerm" {}
Next tun init command in terraform to download azurerm provider
terraform init

Next step is to copy the provider file to the following location
/Users/<user>/.terraform.d/plugins/darwin_amd64/terraform-provider-azurerm_v2.24.0_x5
If you have trouble of finding the provider download it located on the folder root folder of the init command exicuted. In my case its as follows.
Users/<user>/<path to root folder>/.terraform/plugins/registry.terraform.io/hashicorp/azurerm/2.24.0/darwin_amd64
Next we are all set to run the terraformer command. Based on the provider chosen parameters may change. Please refer the help using –help
terraformer import azure -r virtual_machine,disk,network_interface,network_security_group,resource_group,storage_account,virtual_network
-r – Stands for resources you can specify supported resources to be imported as terraform
The above command export all the mentioned resources from the subscription. You can use additional parameters and filters to import specific resources only.
terraformer import azure -r virtual_machine,disk,network_interface,network_security_group,resource_group,storage_account,virtual_network
2020/08/25 23:57:15 Testing if Service Principal / Client Certificate is applicable for Authentication..
2020/08/25 23:57:15 Testing if Multi Tenant Service Principal / Client Secret is applicable for Authentication..
2020/08/25 23:57:15 Testing if Service Principal / Client Secret is applicable for Authentication..
2020/08/25 23:57:15 Using Service Principal / Client Secret for Authentication
2020/08/25 23:57:15 Getting OAuth config for endpoint https://login.microsoftonline.com/ with tenant
2020/08/25 23:57:15 azurerm importing... virtual_machine
2020/08/25 23:57:22 Refreshing state... azurerm_linux_virtual_machine.tfer--MasterNode-002D-01
2020/08/25 23:57:22 Refreshing state... azurerm_linux_virtual_machine.tfer--WorkerNode-002D-01
2020/08/25 23:57:22 Refreshing state... azurerm_linux_virtual_machine.tfer--WorkerNode-002D-02
2020/08/25 23:57:22 Refreshing state... azurerm_windows_virtual_machine.tfer--azurefileswin
2020/08/25 23:57:22 Refreshing state... azurerm_windows_virtual_machine.tfer--WinHost1909
2020/08/25 23:57:26 azurerm importing... disk
2020/08/25 23:57:31 Refreshing state... azurerm_managed_disk.tfer--WinHost1909_OsDisk_1_5c9b1fe692d94b108ba14776662d73fc
2020/08/25 23:57:31 Refreshing state... azurerm_managed_disk.tfer--azurefileswin_disk1_08e7da0b5c7d4d4b98a1c122415aa4b2
2020/08/25 23:57:31 azurerm importing... network_interface
2020/08/25 23:57:37 Refreshing state... azurerm_network_interface.tfer--winhost1909348
2020/08/25 23:57:37 Refreshing state... azurerm_network_interface.tfer--masternode-002D-0131
2020/08/25 23:57:37 Refreshing state... azurerm_network_interface.tfer--workernode-002D-01885
2020/08/25 23:57:37 Refreshing state... azurerm_network_interface.tfer--azurefileswin861
2020/08/25 23:57:37 Refreshing state... azurerm_network_interface.tfer--workernode-002D-02511
2020/08/25 23:57:39 azurerm importing... network_security_group
2020/08/25 23:57:45 Refreshing state... azurerm_network_security_group.tfer--WorkerNode-002D-02-002D-nsg
2020/08/25 23:57:45 Refreshing state... azurerm_network_security_group.tfer--WinHost1909-002D-nsg
2020/08/25 23:57:45 Refreshing state... azurerm_network_security_group.tfer--azurefileswin-002D-nsg
2020/08/25 23:57:45 Refreshing state... azurerm_network_security_group.tfer--WorkerNode-002D-01-002D-nsg
2020/08/25 23:57:45 Refreshing state... azurerm_network_security_group.tfer--MasterNode-002D-01-002D-nsg
2020/08/25 23:57:46 azurerm importing... resource_group
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--vso-002D-rg-002D-7713c23
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--sa-002D-rg
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--MSLearn-002D-Demos
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--AzureFilesDemo
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--cosmosdb
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--mysql
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--DefaultResourceGroup-002D-EUS2
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--DockerHost-002D-RG
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--K8sLab
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--NetworkWatcherRG
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--automation-002D-rg
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--terraformstate-002D-rg
2020/08/25 23:57:56 Refreshing state... azurerm_resource_group.tfer--vm-002D-backuptest
2020/08/25 23:57:56 azurerm importing... storage_account
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--k8slabdiag277
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--terraformstatesana
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--azuefilesdemokasun
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--azurefilesdemodiag452
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--dockerhostrgdiag652
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--kasun31423
2020/08/25 23:58:01 Refreshing state... azurerm_storage_account.tfer--k8slabdisks982
2020/08/25 23:58:08 azurerm importing... virtual_network
2020/08/25 23:58:13 Refreshing state... azurerm_virtual_network.tfer--K8sLab-002D-vnet
2020/08/25 23:58:13 Refreshing state... azurerm_virtual_network.tfer--AzureFilesDemo-002D-vnet
2020/08/25 23:58:13 Refreshing state... azurerm_virtual_network.tfer--DockerHost-002D-RG-002D-vnet
2020/08/25 23:58:14 azurerm Connecting....
2020/08/25 23:58:14 azurerm save storage_account
2020/08/25 23:58:14 azurerm save tfstate for storage_account
2020/08/25 23:58:14 azurerm save virtual_network
2020/08/25 23:58:14 azurerm save tfstate for virtual_network
2020/08/25 23:58:14 azurerm save virtual_machine
2020/08/25 23:58:14 azurerm save tfstate for virtual_machine
2020/08/25 23:58:14 azurerm save disk
2020/08/25 23:58:14 azurerm save tfstate for disk
2020/08/25 23:58:14 azurerm save network_interface
2020/08/25 23:58:14 azurerm save tfstate for network_interface
2020/08/25 23:58:14 azurerm save network_security_group
2020/08/25 23:58:14 azurerm save tfstate for network_security_group
2020/08/25 23:58:14 azurerm save resource_group
2020/08/25 23:58:14 azurerm save tfstate for resource_group
after the export completed you can see a folder called generated inside that folder we can find the imported terraform files. Those files are categorized as the resource type as below.

if we go inside virtual_machine it also categorized based on the operating system.
When open .tf file in editor you can refer all the VMs terraform code sample as follow.
Conclusion
Terraformer tool can help DevOps engineers and developers to import the existing resources terraform output to create and manage existing resources through terraform.