Skip to main content

Generate Terraform import blocks directly from a binary .plan file

Project description

generate-imports-from-plan

Generates Terraform import {} blocks directly from a binary .plan file — no terraform CLI, no provider plugins, no terraform init required.

Why?

terraform show -json does not expose all planned attribute values — computed fields like resource names and resource group names are missing from the JSON output. This tool reads the raw msgpack-encoded attributes directly from the binary plan, giving access to all values needed to construct Azure resource IDs.

Installation

uvx generate-imports-from-plan terraform.plan

Or install permanently:

uv tool install generate-imports-from-plan
generate-imports-from-plan terraform.plan

Workflow

# 1. Generate plan
terraform plan -out terraform.plan

# 2. Generate import blocks and write to file
generate-imports-from-plan terraform.plan > imports.tf

# 3. Fill in any remaining # TODO ids, then re-plan and apply
terraform plan -out terraform.plan
terraform apply terraform.plan

Output

import {
  to = module.core.azurerm_resource_group.this
  id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-myapp-dev-we-01"
}

import {
  to = module.core.azurerm_storage_account.this
  id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-myapp-dev-we-01/providers/Microsoft.Storage/storageAccounts/stmyapp01"
}

Resources whose type is not in the built-in formula table get an empty id with a # TODO comment.

Flags

Flag Description
--skip-imported Skip resources that already have an import {} block in the config
--list Print address\tid pairs instead of HCL blocks
--list --powershell Output a PowerShell array literal of resource addresses
--target ADDR [ADDR ...] Only process the specified resource addresses
--debug Dump all decoded attributes per resource — useful when an ID shows a placeholder

Adding a resource type

If a resource type is missing, extend _ID_FORMULAS in generate_imports.py:

"azurerm_my_resource":
    lambda a, s: _arm(s, _str(a, "resource_group_name"),
                      "Microsoft.MyNamespace/myResources", _str(a, "name")),

_arm(sub, rg, *segments) builds /subscriptions/{sub}/resourceGroups/{rg}/providers/{...}.
_str(attrs, key) returns the attribute value or <key> as a placeholder when unknown.

Use --debug to see all available attribute names for an unrecognised resource type.

Requirements

  • Python 3.11+
  • uv (for uvx)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

generate_imports_from_plan-0.1.3.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

generate_imports_from_plan-0.1.3-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file generate_imports_from_plan-0.1.3.tar.gz.

File metadata

  • Download URL: generate_imports_from_plan-0.1.3.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for generate_imports_from_plan-0.1.3.tar.gz
Algorithm Hash digest
SHA256 cee5841d1a08726f7dd664a93723b6cca681c1f0d8e1a9fc916f07e6f7e3dee4
MD5 a3840ed8e1d6437148a855cfbf8b8ccb
BLAKE2b-256 669417502c14e2b1bbc279154ae8bdf5d029843623a550ece51acfa6d86ea2bd

See more details on using hashes here.

File details

Details for the file generate_imports_from_plan-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: generate_imports_from_plan-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.18 {"installer":{"name":"uv","version":"0.11.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for generate_imports_from_plan-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6e9b03b667d27830f9663b6de7ab1ee8caf06fbeaefa439451c243137f51f96b
MD5 0777eb7ccce6f5bd5077d17962710c07
BLAKE2b-256 3ebbed8852c5df01f9307fa5c7163391ae9054cb30b2d0c99132319cd9db8d3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page