Home / Market / import infrastructure as code

import infrastructure as code

Import existing Azure resources into Terraform using Azure CLI discovery and Azure Verified Modules (AVM). Use when asked to reverse-engineer live Azure infrastructure, generate Infrastructure as Code from existing subscriptions/resource groups/resource IDs, map dependencies, derive exact import addresses from downloaded module source, prevent configuration drift, and produce AVM-based Terraform files ready for validation and planning across any Azure resource type.

skill★ 417940 dlgithubMITInstall in CosmosUpstream Source

Documentation & Preview

--- name: import-infrastructure-as-code description: 'Import existing Azure resources into Terraform using Azure CLI discovery and Azure Verified Modules (AVM). Use when asked to reverse-engineer live Azure infrastructure, generate Infrastructure as Code from existing subscriptions/resource groups/resource IDs, map dependencies, derive exact import addresses from downloaded module source, prevent configuration drift, and produce AVM-based Terraform files ready for validation and planning across any Azure resource type.'

---

Import Infrastructure as Code (Azure -> Terraform with AVM)

Convert existing Azure infrastructure into maintainable Terraform code using discovery data and Azure Verified Modules.

When to Use This Skill

Use this skill when the user asks to:

Prerequisites

Inputs

| Parameter | Required | Default | Description | |---|---|---|---| | subscription-id | No | Active CLI context | Azure subscription used for subscription-scope discovery and context setting | | resource-group-name | No | None | Azure resource group used for resource-group-scope discovery | | resource-id | No | None | One or more Azure ARM resource IDs used for specific-resource-scope discovery |

At least one of subscription-id, resource-group-name, or resource-id is required.

Step-by-Step Workflows

1) Collect Required Scope (Mandatory)

Request one of these scopes before running discovery commands:

Scope handling rules:

If scope is missing, ask for it explicitly and stop.

2) Authenticate and Set Context

Run only the commands required for the selected scope.

For subscription scope:

az login
az account set --subscription <subscription-id>
az account show --query "{subscriptionId:id, name:name, tenantId:tenantId}" -o json

Expected output: JSON object with subscriptionId, name, and tenantId.

For resource group or specific resource scope, az login is still required but az account set is optional if the active context is already correct.

When using specific resource scope, prefer direct --ids-based commands first and avoid extra discovery prompts for subscription or resource group unless needed for a concrete command.

3) Run Discovery Commands

Discover resources using the selected scopes. Ensure to fetch all necessary information for accurate Terraform generation.

# Subscription scope
az resource list --subscription <subscription-id> -o json

# Resource group scope
az resource list --resource-group <resource-group-name> -o json

# Specific resource scope
az resource show --ids <resource-id-1> <resource-id-2> ... -o json

Expected output: JSON object or array containing Azure resource metadata (id, type, name, location, tags, properties).

4) Resolve Dependencies Before Code Generation

Parse exported JSON and map:

IMPORTANT: Generate the following documentation and save it to a docs folder in the root of the project.

5) Select Azure Verified Modules (Required)

Use the latest AVM version for each resource type.

Terraform Registry

Official AVM Index

> Note: The following links always point to the latest version of the CSV files on the main branch. As intended, this means the files may change over time. If you require a point-in-time version, consider using a specific release tag in the URL.

Individual Module information

Use the web tool or another suitable MCP method to get module information if not available locally in the .terraform folder.

Use AVM sources:

Prefer AVM modules over handwritten azurerm_* resources when an AVM module exists.

When fetching module information from GitHub repositories, the README.md file in the root of the repository typically contains all detailed information about the module, for example: https://raw.githubusercontent.com/Azure/terraform-azurerm-avm-res-<service>-<resource>/refs/heads/main/README.md

5a) Read the Module README Before Writing Any Code (Mandatory)

This step is not optional. Before writing a single line of HCL for a module, fetch and read the full README for that module. Do not rely on knowledge of the raw azurerm provider or prior experience with other AVM modules.

For each selected AVM module, fetch its README:

https://raw.githubusercontent.com/Azure/terraform-azurerm-avm-res-<service>-<resource>/refs/heads/main/README.md

Or if the module is already downloaded after terraform init:

cat .terraform/modules/<module_key>/README.md

From the README, extract and record before writing code:

  1. Required Inputs — every input the module requires. Any child resource listed here

(NICs, extensions, subnets, public IPs) is managed inside the module. Do not create standalone module blocks for those resources.

  1. Optional Inputs — the exact Terraform variable names and their declared type.

Do not assume they match the raw azurerm provider argument names or block shapes.

  1. Usage examples — check what resource group identifier is used (parent_id vs

resource_group_name), how child resources are expressed (inline map vs separate module), and what syntax each input expects.

Apply module rules as patterns, not assumptions

Use the lessons below as examples of the *type* of mismatch that often causes imports to fail. Do not assume these exact names apply to every AVM module. Always verify each selected module's README and variables.tf.

avm-res-compute-virtualmachine (any version)

create standalone avm-res-network-networkinterface modules alongside a VM module — define every NIC inline under network_interfaces.

and vtpm_enabled = true. The security_type argument exists only under os_disk for Confidential VM disk encryption and must not be used for TrustedLaunch.

separate boot_diagnostics_storage_account_uri variable if a storage URI is needed.

standalone extension resources.

avm-res-network-virtualnetwork (any version)

resource group resource ID string) to specify the resource group, not resource_group_name.

Generalized takeaway for all AVM modules:

6) Generate Terraform Files

Before Writing Import Blocks — Inspect Module Source (Mandatory)

After terraform init downloads the modules, inspect each module's source files to determine the exact Terraform resource addresses before writing any import {} blocks. Never write import addresses from memory.

Step A — Identify the provider and resource label

grep "^resource" .terraform/modules/<module_key>/main*.tf

This reveals whether the module uses azurerm_* or azapi_resource labels. For example, avm-res-network-virtualnetwork exposes azapi_resource "vnet", not azurerm_virtual_network "this".

Step B — Identify child modules and nested paths

grep "^module" .terraform/modules/<module_key>/main*.tf

If child resources are managed in a sub-module (subnets, extensions, etc.), the import address must include every intermediate module label:

module.<root_module_key>.module.<child_module_key>["<map_key>"].<resource_type>.<label>[<index>]

Step C — Check for count vs for_each

grep -n "count\|for_each" .terraform/modules/<module_key>/main*.tf

Any resource using count requires an index in the import address. When count = 1 (e.g., conditional Linux vs Windows selection), the address must end with [0]. Resources using for_each use string keys, not numeric indexes.

Known import address patterns (examples from lessons learned)

These are examples only. Use them as templates for reasoning, then derive the exact addresses from the downloaded source code for the modules in your current import.

| Resource | Correct import to address pattern | |---|---| | AzAPI-backed VNet | module.<vnet_key>.azapi_resource.vnet | | Subnet (nested, count-based) | module.<vnet_key>.module.subnet["<subnet_name>"].azapi_resource.subnet[0] | | Linux VM (count-based) | module.<vm_key>.azurerm_linux_virtual_machine.this[0] | | VM NIC | module.<vm_key>.azurerm_network_interface.virtualmachine_network_interfaces["<nic_key>"] | | VM extension (default deploy_sequence=5) | module.<vm_key>.module.extension["<ext_name>"].azurerm_virtual_machine_extension.this | | VM extension (deploy_sequence=1–4) | module.<vm_key>.module.extension_<n>["<ext_name>"].azurerm_virtual_machine_extension.this | | NSG-NIC association | module.<vm_key>.azurerm_network_interface_security_group_association.this["<nic_key>-<nsg_key>"] |

Produce: