Convert Figma logo to code with AI

Azure logoaztfexport

A tool to bring existing Azure resources under Terraform's management

1,782
209
1,782
41

Top Related Projects

Terraform provider for Azure Resource Manager

Microsoft Azure PowerShell

Azure Command-Line Interface

Quick Overview

Azure/aztfexport is an open-source tool that generates Terraform configurations from existing Azure resources. It allows users to reverse-engineer their Azure infrastructure into Terraform code, facilitating infrastructure-as-code practices and making it easier to manage and version control Azure resources.

Pros

  • Simplifies the process of adopting Terraform for existing Azure infrastructures
  • Supports a wide range of Azure resource types
  • Generates human-readable and well-formatted Terraform code
  • Helps in documenting and version-controlling existing Azure resources

Cons

  • May not capture all nuances of complex Azure configurations
  • Generated code might require manual adjustments for optimal use
  • Limited to Azure resources only, not applicable for multi-cloud setups
  • Dependency on Azure CLI for authentication and resource discovery

Code Examples

# Example of generated Terraform code for an Azure Resource Group
resource "azurerm_resource_group" "example" {
  name     = "example-resource-group"
  location = "eastus"
  tags = {
    environment = "production"
  }
}
# Example of generated Terraform code for an Azure Virtual Network
resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}
# Example of generated Terraform code for an Azure Storage Account
resource "azurerm_storage_account" "example" {
  name                     = "examplestorageaccount"
  resource_group_name      = azurerm_resource_group.example.name
  location                 = azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}

Getting Started

  1. Install Azure CLI and log in to your Azure account:

    az login
    
  2. Install aztfexport:

    go install github.com/Azure/aztfexport@latest
    
  3. Run aztfexport to generate Terraform configurations:

    aztfexport -o ./output_directory
    
  4. Review and adjust the generated Terraform files in the output directory as needed.

Competitor Comparisons

Terraform provider for Azure Resource Manager

Pros of terraform-provider-azurerm

  • More comprehensive coverage of Azure resources
  • Mature and widely adopted in the Terraform ecosystem
  • Extensive documentation and community support

Cons of terraform-provider-azurerm

  • Requires manual writing of Terraform configurations
  • Learning curve for Terraform language and provider syntax
  • May not always reflect the latest Azure resource updates immediately

Code Comparison

terraform-provider-azurerm:

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

aztfexport:

resource "azurerm_resource_group" "exported" {
  name     = "example-resources"
  location = "West Europe"
}

The code comparison shows that both tools generate similar Terraform configurations. However, aztfexport automatically generates these configurations from existing Azure resources, while terraform-provider-azurerm requires manual writing.

terraform-provider-azurerm is the official Azure provider for Terraform, offering a wide range of resource types and data sources. It's well-maintained and regularly updated, but requires users to write Terraform configurations manually.

aztfexport, on the other hand, is a tool designed to export existing Azure resources into Terraform configurations. It's particularly useful for migrating existing infrastructure to Terraform or generating initial configurations, but may not cover all possible Azure resources or complex scenarios.

Microsoft Azure PowerShell

Pros of azure-powershell

  • Extensive coverage of Azure services and features
  • Well-established and mature project with a large community
  • Supports both Windows PowerShell and PowerShell Core

Cons of azure-powershell

  • Steeper learning curve for those unfamiliar with PowerShell
  • Can be more verbose for simple tasks compared to Terraform
  • May require more manual scripting for complex deployments

Code Comparison

azure-powershell:

New-AzResourceGroup -Name "MyResourceGroup" -Location "EastUS"
New-AzStorageAccount -ResourceGroupName "MyResourceGroup" -Name "mystorageaccount" -Location "EastUS" -SkuName "Standard_LRS"

aztfexport:

resource "azurerm_resource_group" "example" {
  name     = "MyResourceGroup"
  location = "EastUS"
}

resource "azurerm_storage_account" "example" {
  name                = "mystorageaccount"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  account_tier        = "Standard"
  account_replication_type = "LRS"
}

Summary

azure-powershell offers a comprehensive set of cmdlets for managing Azure resources, with broad service coverage and strong community support. It's particularly useful for administrators familiar with PowerShell. aztfexport, on the other hand, focuses on exporting existing Azure resources to Terraform configurations, which can be beneficial for teams transitioning to Infrastructure as Code practices or those preferring Terraform's declarative approach.

Azure Command-Line Interface

Pros of azure-cli

  • Comprehensive CLI tool for managing all Azure services
  • Extensive documentation and community support
  • Regularly updated with new Azure features and improvements

Cons of azure-cli

  • Larger footprint and more complex installation process
  • Steeper learning curve for users new to Azure
  • May include unnecessary functionality for specific use cases

Code Comparison

azure-cli:

from azure.cli.core import get_default_cli

az_cli = get_default_cli()
az_cli.invoke(['vm', 'create', '--name', 'myVM', '--resource-group', 'myRG'])

aztfexport:

func main() {
    client := aztfexport.NewClient()
    err := client.ExportResourceGroup("myRG", "main.tf")
    if err != nil {
        log.Fatal(err)
    }

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Microsoft Azure Export for Terraform

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

Video (v0.12.0)

Video

Goal

Azure Export for Terraform exports supported resources into Terraform state and generate the corresponding Terraform configuration. Both the Terraform state and configuration are expected to 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.

It supports both the Terraform AzureRM provider and the Terraform AzAPI provider.

Non Goal

The Terraform configurations generated by aztfexport are not meant to be comprehensive and do not ensure that the infrastructure can be fully reproduced from said generated configurations. For details, please see limitations).

Install

From Release

Precompiled binaries and Window MSI are available at Releases.

From Go toolchain

go install github.com/Azure/aztfexport@latest

From Package Manager

Windows

winget install aztfexport

Homebrew (Linux/macOS)

brew install aztfexport

dnf (Linux)

Supported versions:

  • RHEL 8 (amd64, arm64)
  • RHEL 9 (amd64, arm64)
  1. Import the Microsoft repository key:

    rpm --import https://packages.microsoft.com/keys/microsoft.asc
    
  2. Add packages-microsoft-com-prod repository:

    ver=8 # or 9
    dnf install -y https://packages.microsoft.com/config/rhel/${ver}/packages-microsoft-prod.rpm
    
  3. Install:

    dnf install aztfexport
    

apt (Linux)

Supported versions:

  • Ubuntu 20.04 (amd64, arm64)
  • Ubuntu 22.04 (amd64, arm64)
  1. Import the Microsoft repository key:

    curl -sSL https://packages.microsoft.com/keys/microsoft.asc > /etc/apt/trusted.gpg.d/microsoft.asc
    
  2. Add packages-microsoft-com-prod repository:

    ver=20.04 # or 22.04
    apt-add-repository https://packages.microsoft.com/ubuntu/${ver}/prod
    
  3. Install:

    apt-get install aztfexport
    

AUR (Linux)

yay -S aztfexport

Prerequisites

aztfexport requires a terraform executable installed in the $PATH with version >= v0.12.

How it Works

aztfexport leverages aztft to identify the Terraform resource type corresponding to an Azure resource ID. Then it runs terraform import under the hood to import each resource. Afterwards, it runs tfadd to generate the Terraform HCL code for each imported resource.

Usage

Read the Azure Export documentation which covers scenarios and usage.

Config

aztfexport will create a configuration file at $HOME/.aztfexport/config.json. This file is aim to be managed by command aztfexport config [subcommand], which includes following subcommands:

  • get: Get a config item
  • set: Set a config item
  • show: Show the full configuration

Currently, the following config items are supported:

  • installation_id: A UUID created on first run. If there is Azure CLI or Azure Powershell installed on the current machine, the UUID will be the same value among these tools. Otherwise, a new one will be created. This is used as an identifier in the telemetry trace.
  • telemetry_enabled: Enables telemetry. We use telemetry to identify issues and areas for improvement, in order to optimize this tool for better performance, reliability, and user experience. If you wish to disable our telemetry, set this to false.

Limitations

Visit this page on the Azure Export for Terraform documentation that discusses the currently known limitations of the tool.

Additional Resources

  • The aztfexport Github Page: Everything about aztfexport, including comparisons with other existing import solutions.
  • aztft: A Go program and library for identifying the correct Terraform AzureRM provider resource type on the Azure resource id.
  • tfadd: A Go program and library for generating Terraform configuration from Terraform state.