aztfexport

repository·main·Indexed 23 days ago

https://github.com/azure/aztfexport

A tool designed to export existing Azure resources into Terraform state and generate the corresponding Terraform HCL configuration. It automates the process of bringing Azure infrastructure under Terraform management by mapping resource IDs to Terraform types, executing terraform import, and generating HCL code to ensure consistency between the remote Azure state and the local configuration.

Tokens
4.1K
Snippets
11
Records
35
Agent score
83%

What's inside aztfexport

  1. How aztfexport works

    main

    The tool automates the process of bringing existing Azure resources under Terraform management by performing the following steps:

    1. Identification: Uses aztft to map an Azure resource ID to the corresponding Terraform resource type.
    2. Import: Executes terraform import under the hood for each identified resource.
    3. Configuration Generation: Uses tfadd to generate the Terraform HCL code for each imported resource.

    The goal is to produce a Terraform state and configuration that are consistent with the remote Azure state, such that a terraform plan shows no differences.

  2. Install aztfexport

    main

    You can install aztfexport using several methods depending on your operating system and preferred toolchain:

    Go toolchain

    go install github.com/Azure/aztfexport@latest

    Windows

    • Release: Download precompiled binaries or MSI from the Releases page.
    • Winget:
      winget install aztfexport

    macOS and Linux

    • Homebrew:
      brew install aztfexport
    • AUR (Arch Linux):
      yay -S aztfexport
    • dnf (RHEL 8/9):
      1. Import key: rpm --import https://packages.microsoft.com/keys/microsoft.asc
      2. Add repo: dnf install -y https://packages.microsoft.com/config/rhel/${ver}/packages-microsoft-prod.rpm (replace ${ver} with 8 or 9)
      3. Install: dnf install aztfexport
    • apt (Ubuntu 20.04/22.04):
      1. Import key: curl -sSL https://packages.microsoft.com/keys/microsoft.asc > /etc/apt/trusted.gpg.d/microsoft.asc
      2. Add repo: apt-add-repository https://packages.microsoft.com/ubuntu/${ver}/prod (replace ${ver} with 20.04 or 22.04)
      3. Install: apt-get install aztfexport
    go install github.com/Azure/aztfexport@latest
  3. Configure authentication for aztfexport

    main

    Authentication is managed via the AuthConfig struct, which is populated from various CLI flags. You can authenticate using several methods:

    Service Principal Authentication

    • Client ID: Use --client-id or --client-id-file-path.
    • Client Secret: Use --client-secret or --client-secret-file-path.
    • Client Certificate: Use --client-certificate (base64 encoded) or --client-certificate-path. You can also provide a password via --client-certificate-password.
    • Tenant ID: Use --tenant-id. You can also specify multiple tenants using --tenant-id multiple times (auxiliary tenants).

    Other Authentication Methods

    • Azure CLI: Use --use-azure-cli-cred to leverage your local Azure CLI session.
    • Managed Identity: Use --use-managed-identity-cred for environments supporting Azure Managed Identities.
    • OIDC: Use --use-oidc-cred along with --oidc-request-token, --oidc-request-url, or --oidc-token-file-path for OpenID Connect based authentication.
  4. Manage aztfexport configuration

    main

    Configuration is stored in $HOME/.aztfexport/config.json. You should manage this file using the aztfexport config command.

    Subcommands

    • get: Retrieve a specific configuration item.
    • set: Update a configuration item.
    • show: Display the entire configuration.

    Supported Config Items

    • installation_id: A UUID used as an identifier in telemetry traces. If Azure CLI or Azure PowerShell is present, this ID remains consistent across those tools.
    • telemetry_enabled: A boolean to enable or disable telemetry. Set to false to opt-out.
  5. Use aztfexportclient for Bubble Tea UI communication

    main

    The aztfexportclient package provides a set of tea.Cmd functions designed to be used within a Bubble Tea application. These functions wrap operations performed on a meta.Meta instance and return messages (tea.Msg) that the Bubble Tea program can handle to update its state or trigger UI changes.

    Each function typically follows this pattern:

    1. It accepts a context.Context and a meta.Meta object.
    2. It returns a tea.Cmd.
    3. When executed by the Bubble Tea runtime, the command performs the requested operation (e.g., listing resources, importing items, generating configuration).
    4. It returns either an ErrMsg on failure or a specific success message (e.g., ListResourceDoneMsg, ImportItemsDoneMsg) on success.
  6. Understand aztfexport execution modes

    main

    The aztfexport CLI operates in several distinct modes, which determine the scope and target of the export process. These modes are defined by the Mode type:

    • resource: Export a specific Azure resource.
    • resource-group: Export all resources within a specific resource group.
    • query: Export resources based on a query pattern.
    • mapping-file: Use a mapping file to drive the export.
  7. Use aztfexport commands to export Azure resources

    main

    The aztfexport CLI provides several modes for bringing Azure resources under Terraform management. The general syntax is aztfexport <command> [option] <scope>.

    Available Commands

    • resource: Export specific resources. Arguments can be individual resource IDs or files containing resource IDs (prefixed with @).
      • Usage: aztfexport resource [option] [<resourceId> | @<resourceIdFile>...]
    • resource-group: Export an entire resource group and all its nested resources.
      • Usage: aztfexport resource-group [option] <resource group name>
    • query: Export resources determined by an Azure Resource Graph (ARG) predicate.
      • Usage: aztfexport query [option] <ARG where predicate>
    • mapping-file: Export resources based on a provided resource mapping file.
      • Usage: aztfexport mapping-file [option] <resource mapping file>
    • config: Manage tool configuration.
      • Usage: aztfexport config [set|get|show]
  8. Configure OidcCredentialOptions

    main

    When initializing an OidcCredential via NewOidcCredential, use the OidcCredentialOptions struct to provide the necessary authentication parameters:

    • TenantID: The Azure AD tenant ID.
    • ClientID: The application (client) ID.
    • RequestToken: The bearer token used to authorize the request to the OIDC provider.
    • RequestUrl: The URL from which to fetch the JWT assertion.
    • Token: (Optional) If provided, this static token is used directly as the assertion instead of making an HTTP request.
    • ClientOptions: Standard azcore.ClientOptions for the underlying Azure SDK client.
    type OidcCredentialOptions struct {
    	azcore.ClientOptions
    	TenantID     string
    	ClientID     string
    	RequestToken string
    	RequestUrl   string
    	Token        string
    }