Skip to content

Instantly share code, notes, and snippets.

@hkboujrida
Created January 14, 2024 20:53
Show Gist options
  • Save hkboujrida/b5a40a8e00cc1fb3d8ee6c11db7c1f80 to your computer and use it in GitHub Desktop.
Save hkboujrida/b5a40a8e00cc1fb3d8ee6c11db7c1f80 to your computer and use it in GitHub Desktop.
autoimport
# terraformer import azure -R edgeprime-oss -r subnet
# terraform plan -out=tfplan
# terraform show -json tfplan > plan.json
# bash script to automatically import state to terraform
#!/bin/bash
# get all the ressources id and name from the state file
file="imported.tfstate"
# jq '.modules[].resources | .[] | {type: .type, id: .primary.id, name: .primary.attributes.name}' "$file"
# put the ressources id and name, and type with jq into the variable ressources
existing_ressources=$(jq '[.modules[].resources | .[] | { type: .type, name: .primary.attributes.name, id: .primary.id}]' imported.tfstate)
# example of output
# [
# {
# "type": "azurerm_subnet",
# "name": "default",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/default"
# },
# {
# "type": "azurerm_subnet",
# "name": "subnet1",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet1"
# },
# {
# "type": "azurerm_subnet",
# "name": "subnet2",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet2"
# },
# {
# "type": "azurerm_subnet",
# "name": "subnet3",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet3"
# }
# ]
tfplan="plan.json"
planned_ressources=$(jq '[.planned_values.root_module.resources | .[] | {name: .index, type: .type, address: .address }]' "$tfplan")
# example of output
# [
# {
# "name": "default",
# "type": "azurerm_subnet",
# "address": "azurerm_subnet.gov[\"default\"]"
# },
# {
# "name": "subnet1",
# "type": "azurerm_subnet",
# "address": "azurerm_subnet.gov[\"subnet1\"]"
# },
# {
# "name": "subnet2",
# "type": "azurerm_subnet",
# "address": "azurerm_subnet.gov[\"subnet2\"]"
# },
# {
# "name": "subnet3",
# "type": "azurerm_subnet",
# "address": "azurerm_subnet.gov[\"subnet3\"]"
# }
# ]
# join the two json array existing_ressources and planned_ressources grouped by name and type
ressources_to_import=$(jq -n --argjson existing_ressources "$existing_ressources" --argjson planned_ressources "$planned_ressources" '($existing_ressources + $planned_ressources) | group_by(.name, .type) | map({name: .[0].name, type: .[0].type, id: .[0].id, address: .[1].address})')
# example of output
# [
# {
# "name": "default",
# "type": "azurerm_subnet",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/default",
# "address": "azurerm_subnet.gov[\"default\"]"
# },
# {
# "name": "subnet1",
# "type": "azurerm_subnet",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet1",
# "address": "azurerm_subnet.gov[\"subnet1\"]"
# },
# {
# "name": "subnet2",
# "type": "azurerm_subnet",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet2",
# "address": "azurerm_subnet.gov[\"subnet2\"]"
# },
# {
# "name": "subnet3",
# "type": "azurerm_subnet",
# "id": "/subscriptions/d33e5a70-b90d-4231-bb11-70f99afedbae/resourceGroups/edgeprime-oss/providers/Microsoft.Network/virtualNetworks/edgeprime-vnet/subnets/subnet3",
# "address": "azurerm_subnet.gov[\"subnet3\"]"
# }
# ]
# terraform state import all the ressources in the variable ressources_to_import based on the address and id
echo "$ressources_to_import" | jq -r '.[] | "terraform import \(.address) \(.id)"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment