Quickstart: Export your first resources using Azure Export for Terraform

This article shows how to export Azure resources into local state files using Azure Export for Terraform.

  • Create a test Azure resource group using Azure CLI or Azure PowerShell.
  • Create a test Linux virtual machine using Azure CLI or Azure PowerShell.
  • Export the state for the resource group and virtual machine from Azure to the local state file.
  • Test that the local state matches the state of the resources in Azure.

Prerequisites

Create the test Azure resources

Create a Linux VM.

  1. Run az group create to create an Azure resource group.

    az group create --name myResourceGroup --location eastus
    
  2. Run az vm create to create the virtual machine.

    az vm create \
      --resource-group myResourceGroup \
      --name myVM \
      --image Debian11 \
      --admin-username azureadmin \
      --generate-ssh-keys \
      --public-ip-sku Standard
    

Export an Azure resource

You can run the aztfexport tool in one of two modes: interactive and non-interactive. For this demo, you use the interactive mode.

  1. Create a directory in which to test.

  2. Open a command prompt and navigate to the new directory.

  3. Run aztfexport resource-group to export the resource group named myResourceGroup.

    aztfexport resource-group myResourceGroup
    
  4. After the tool initializes, a list of the resources to be exported is displayed. Each line has an Azure resourceID matched to the corresponding AzureRM resource type. The list of available commands displays at the bottom of the display. Using one of the commands, scroll to the bottom and verify that the expected Azure resources are properly mapped to their respective Terraform resource types.

    Screenshot showing the Azure resources that would  be exported based on the command and resource specified when you run the Azure Export for Terraform tool.

  5. Press w to run the export.

    Key points:

    • For a non-interactive resource, add the --non-interactive flag: aztfexport rg --non-interactive myResourceGroup.

Note

Running Azure Export for Terraform can take several minutes to complete.

Verify the results

After the tool has finished exporting your Azure resources, run the following commands in the same directory that contains the generated files.

  1. Run terraform init.

    terraform init --upgrade
    
  2. Run terraform plan.

    terraform plan
    

If the terminal outputs No changes needed, then congratulations!

Your infrastructure and its corresponding state have been successfully exported to Terraform.

Clean up resources

When you no longer need the resources created in this article, do the following steps:

  1. Navigate to the directory containing your Terraform files for this article.

  2. Run terraform destroy.

    terraform destroy
    

Next steps