Import ADF in Terraform state

Khandu Shinde 130 Reputation points
2023-12-04T05:44:59.43+00:00

I have Azure Data Factory which was created manually. It consists of Linked services, runtime integrations and few triggers. Now I want to import all these configurations into terraform state file. I have tried below things.

resource "azurerm_data_factory" "adf" {
  name                = "project-apps-${var.env_info.name}-df"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
}

terraform import 'azurerm_data_factory.adf' 'ADF resource ID'

Although above block imported ADF related content but not everything what it have currently. i.e.

Linked services, runtime integrations and triggers.

Could you please let me know how do I import everything about particular resource?

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,625 questions
0 comments No comments
{count} votes

Accepted answer
  1. Luis Arias 8,621 Reputation points Volunteer Moderator
    2023-12-04T14:23:11.5633333+00:00

    Hi Khandu Shinde,

    You can use aztfexport (tool developed by Microsoft Azure), the best option automatically . Additionally you can do a mix of manual and automatically import:

    1. login to azure :
      az login
    
    1. run azcli first to get all IDs and types from resources on your resource group:
      az resource list --resource-group <Your Resource Group> --query "[].{id:id, type:type}" -o tsv
    
    1. Create the blocks required for the IDs on your terraform code, current implementation of Terraform import can only import resources into the state. It does not generate configuration.Therefore it is necessary to write a resource configuration block before running terraform import , Here you hace two options :
      • Create resource manually from microsoft indications and terraform registry:

      https://learn.microsoft.com/en-us/azure/templates/<Resource Typee from the output> https://registry.terraform.io/

      • Create automatically code with aztfexport: install aztfexport from the release (https://github.com/Azure/aztfexport), run aztfexport: Open a command prompt on path where you want to export the resources. Then, run the following command :
          aztfexport resource-group --name <resource-group-name>
      

    https://learn.microsoft.com/en-us/azure/developer/terraform/azure-export-for-terraform/export-first-resources

    1. Now you can import the configurations into your Terraform state file:
         # Example:
         terraform import azurerm_data_factory_integration_runtime_azure.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.DataFactory/factories/example/integrationruntimes/example
      
    2. Finally, you can run terraform plan to ensure that the import was successful and that your Terraform state matches your actual infrastructure.

    Let me know if this help you. Luis


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.