terraformer to generate terraform file from existing azure infrastructure - unix1998/technical_notes GitHub Wiki

Terraformer to generate Terraform configuration files (.tf files) from existing infrastructure in Azure. Terraformer is an open-source CLI tool that allows you to reverse-engineer infrastructure managed by various cloud providers, including Azure, and generate the corresponding Terraform code.

Here’s a high-level guide on how to use Terraformer for Azure:

  1. Install Terraformer: You can install Terraformer by following the instructions in its GitHub repository. For example, if you are using a system with Homebrew (macOS), you can install it using:

    brew install terraformer
    
  2. Configure Azure CLI: Make sure you have the Azure CLI installed and configured. You can install it from the Azure CLI installation page. Log in to your Azure account:

    az login
    
  3. Generate Terraform configuration: Use Terraformer to generate the Terraform configuration for your Azure resources. For example, to generate Terraform files for all resources in a specific Azure resource group, you can use:

    terraformer import azure -r resource_group -f <resource_group_name>
    

    Replace <resource_group_name> with the name of your resource group.

  4. Review and Organize: Terraformer will generate Terraform configuration files and state files. Review these files to ensure they match your expectations. You might need to organize and refactor the generated code to follow best practices and clean up any unnecessary configurations.

  5. Integrate with Your Terraform Workflow: After generating the Terraform configuration files, integrate them into your existing Terraform workflows. This might involve creating a new Terraform project, storing the state file in a remote backend, and running terraform plan and terraform apply to manage your infrastructure.

Example Command

Here’s a more concrete example command to import resources from a resource group:

terraformer import azure -r virtual_machine,network_interface,storage_account -f my-resource-group

This command tells Terraformer to import virtual machines, network interfaces, and storage accounts from the specified resource group.

Additional Notes

  • Ensure that the Azure CLI is authenticated and has the necessary permissions to access the resources you want to import.
  • Terraformer might not support all Azure resources, so check the Terraformer documentation for the list of supported resources.
  • The generated Terraform code might need some manual adjustments and refinements to meet your specific requirements and best practices.

Using Terraformer can significantly speed up the process of adopting Terraform for existing infrastructure, providing a good starting point for further development and management using Infrastructure as Code (IaC) principles.