top of page

LetsDevOps: Learn to Setup and Run Azure Terrafy. Import Terraform State and Configurations files.

Introduction

In this article we will learn how to use the Azure Terrafy tool to Import the Azure Infrastructure into Terraform configuration Files.


If you are new to Terraform you can go through this article.



Azure Terrafy helps to Create the Configuration file for :

  1. Azure Resource Group and all the resources under it.

  2. Azure Resource

As part of the Process it first import all the resources to state file and then convert that into the configuration file.


Azure Terrafy Tool can be find here:



Architecture


Workflow

aztfy can be run through the command line.
aztfy automatically identify existing resources reside in the resource group.
aztfy automatically import each resource to the state, and convert the state to a valid terraform configuration.


Setup Azure Terrafy

  1. Get the Azure Terrafy executable from below location and you save it to your System .https://github.com/Azure/aztfy/releases/tag/v0.6.0



2. Update you Environment Setting path with aztfy Executable. Reboot the system to complete the change.

3. Create the provider.tf to directory from you want to run the aztfy exe and update the Service principle for the Authentication



terraform {
  backend "local" {}
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.14.0"
    }
  }
}

provider "azurerm" {
  features {}

  subscription_id             = "<Add Subscription ID>"
  client_id                   = "<Add client ID>"
  client_secret               = "<Add Client Secret>"
  tenant_id                   = "<Add Tenant ID>"

}

4. As for example I have created folder in my local C:\Projects\azureterrafyObject to run the Azure Terrafy.

which has only provider.tf with Azure Provider credential like above example.


Run Azure Terrafy - Azure Resource Group

Once the setup completes now we can run the aztfy to get the Terraform configuration and state file.


Assume I want to import the Terraform configuration file of Azure Resource group which has Azure Virtual Machine. Since this Resource group created manually we want to create the Terraform file for this.


Resource Group: devops-test-2_group

Note: Make sure the Resource Group is under selected Service Principle and Subscription in provider.tf file else it will fail for Authentication.


1. Terraform init

Run the Terraform init to initialize the provider.

terraform init



2. Verify if the Azure Terrafy is available.

aztfy -version

2. Run the Import for Azure Resource Group.


aztfy resource-group "devops-test-2_group"






3. Enter w for Import


4. Once the Import completes it will generate the configuration file.



5. On successful run you can see the folder with Terraform State file and configuration file.


Run Azure Terrafy - Azure Single Resource


1. Get the Azure Resource ID


2. Run Below command

aztfy resource <resourceID>

Example:

aztfy resource /subscriptions/000/resourceGroups/devops-test-2_group/providers/Microsoft.Compute/virtualMachines/devops-srstest0103

bottom of page