$ cd ../writing

2026-11-30 · 6 min

Import from the list, not from a string. A hardcoded ID can adopt the wrong repository.

A list of repositories drives a for_each, and each repository is made in GitHub before its name reaches the list, so the next plan tries to create it. One import block, fed from the same list, adopts all of them. A hardcoded import ID next to it can point the import at the wrong repository, and the plan shows no error.

A list of repositories in a JSON file drives a for_each, and Terraform keeps every repository’s settings. The repositories themselves are made outside Terraform. Someone creates one in GitHub and then adds its name to the list. The next plan wants to create the repository, and the apply fails, because GitHub already has a repository with that name.

This is part 3 of a series on Terraform state surgery, which means changing what Terraform manages without destroying anything live, with the plan as the proof. Part 1 took one item out of a for_each, and Part 2 renamed one. The plans below were reproduced with terraform_data on Terraform 1.8 and 1.17.

The setup

The configuration reads the list and declares one repository per entry, with the settings it enforces. One setting stands in for all of them here:

locals {
  repos = jsondecode(
    file("repos.json")
  ).repositories
}

resource "github_repository" "repo" {
  for_each   = toset(local.repos)
  name       = each.key
  has_issues = false
}

acme-web already exists in GitHub and is now in the list. Without an import, the plan wants to create it, and the apply fails because the name is taken:

  # github_repository.repo["acme-web"] will be created
Plan: 1 to add, 0 to change, 0 to destroy.

A plan that adds something you know exists is a missing import.

One import block for the whole list

The import ID of a repository is its name (provider docs). The list already holds every name, so one import block covers every repository in it. Terraform 1.7 and newer accept for_each on an import block. Put the block below in imports.tf. The rest of this article calls it the list block:

import {
  for_each = toset(local.repos)
  to       = github_repository.repo[each.key]
  id       = each.key
}

The plan adopts the new repository and changes it in place, because a repository made outside Terraform does not have the configured settings yet. GitHub turns issues on for a new repository, and the configuration turns them off. Repositories already in state are left alone:

  # github_repository.repo["acme-web"] will be updated in-place
  # (imported from "acme-web")
Plan: 1 to import, 0 to add, 1 to change, 0 to destroy.

After the apply, the repository has the configured settings and the plan says “No changes”. The block stays, and the next repository made outside Terraform is adopted by one new line in the list, with no new code.

When the ID is not a name

A ruleset’s import ID is the repository name and a number that GitHub gives the ruleset, joined by a colon. For example, acme-api:4412. The list does not hold that number, and a hardcoded number fits one ruleset only.

A lookup finds the numbers instead. An external data source runs a script that asks the GitHub API for the rulesets of each repository. It returns a map from "<repository>/<ruleset name>" to the ID. The block below adopts the ruleset named main. Its target, github_repository_ruleset.main, has one entry per repository, like the repository resource:

data "external" "rulesets" {
  program = ["bash", "${path.module}/rulesets.sh"]
  query = {
    repositories = join(",", local.repos)
  }
}

locals {
  ruleset_ids = data.external.rulesets.result
}

import {
  for_each = {
    for repo in local.repos : repo => local.ruleset_ids["${repo}/main"]
    if contains(keys(local.ruleset_ids), "${repo}/main")
  }
  to = github_repository_ruleset.main[each.key]
  id = "${each.key}:${each.value}"
}

A repository that has a ruleset named main gets it imported. A repository without one is not in the map, so the plan creates its ruleset.

The lookup runs at plan time, so it cannot see anything the same apply creates.

A hardcoded ID next to the list block

Say someone adds acme-blog to the list and, out of habit, also writes a hardcoded import block for it. They copy an old block for acme-blog-old, a repository that left Terraform long ago. They paste it into manual.tf and change the address to acme-blog. They forget to change the ID:

import {
  to = github_repository.repo["acme-blog"]
  id = "acme-blog-old"
}

Now two blocks give the address acme-blog two different IDs. The list block gives acme-blog, and manual.tf gives acme-blog-old. Terraform does not stop the plan or warn. It picks one block. Terraform 1.8 takes the block from the file whose name sorts last, and 1.17 the one that sorts first:

  • imports.tf and manual.tf: Terraform 1.8 takes acme-blog-old, and 1.17 takes acme-blog.
  • imports.tf and a-manual.tf: Terraform 1.8 takes acme-blog, and 1.17 takes acme-blog-old.

On 1.8 with manual.tf, the plan reads the old repository:

github_repository.repo["acme-blog"]: Preparing import... [id=acme-blog-old]

The imported repository is named acme-blog-old, and the configuration says acme-blog. The GitHub provider renames a repository in place, without replacing it, so the plan proposes to rename acme-blog-old to acme-blog. The plan shows no error, and nothing in it says that the ID came from a forgotten block. A version upgrade or a renamed file is enough to point the import at the wrong repository.

Once an address is in state, Terraform no longer checks the ID of its import block. Correct a wrong ID afterwards, and the plan still says “No changes”.

Two hardcoded blocks for one address do stop the plan, with “Duplicate import configuration”. A hardcoded block next to the list block does not.

So do not hardcode import IDs. Feed every import block from the list, or from a lookup.

No hardcoded IDs in a shared module

The same rule holds for a module that several roots call. A hardcoded import ID in that module lands in the plan of every caller. The module cannot know what each caller made outside Terraform, so every caller tries to adopt the same object. Keep import blocks out of shared modules.

Note: Terraform 1.16 and newer accept import blocks inside a module. Terraform 1.15.0 to 1.15.8 accept them and ignore them, so the plan creates the object. Terraform 1.15.9 and versions before 1.15 stop the plan with “Import blocks are only allowed in the root module”.

Put the import block in each caller’s root, and feed it from that root’s own list or lookup. Its to reaches into the module, the same way a moved block does:

import {
  for_each = toset(local.repos)
  to       = module.repos.github_repository.repo[each.key]
  id       = each.key
}

Try it without a provider

terraform_data stands in for the repository, with no provider and no cloud account. Put the list in repos.json:

{ "repositories": ["acme-api", "acme-docs"] }

Put the resource in main.tf:

locals {
  repos = jsondecode(file("repos.json")).repositories
}

resource "terraform_data" "repo" {
  for_each = toset(local.repos)
  input    = { has_issues = false }
}

input stands for the configured settings, which the imported object does not have yet. Put the list block in imports.tf:

import {
  for_each = toset(local.repos)
  to       = terraform_data.repo[each.key]
  id       = each.key
}

Run terraform init and terraform plan:

  # terraform_data.repo["acme-api"] will be updated in-place
  # (imported from "acme-api")
  # terraform_data.repo["acme-docs"] will be updated in-place
  # (imported from "acme-docs")
Plan: 2 to import, 0 to add, 2 to change, 0 to destroy.

After terraform apply, the next plan says “No changes”. Add "acme-web" to the list, and the plan shows 1 to import, 0 to add and 1 to change.

To see which ID wins, add "acme-blog" to the list and put this in manual.tf:

import {
  to = terraform_data.repo["acme-blog"]
  id = "acme-blog-old"
}

The plan prints Preparing import... [id=...] for acme-blog. Compare that ID on 1.8 and on 1.17, then rename manual.tf to a-manual.tf and compare again.

What this bought, and what stays manual

Each name is in one place, the list. Adopting a repository made outside Terraform is one line in that list. No import ID is hardcoded, none sits in a shared module, and the block never has to be removed. The price is a lookup script for every ID that is not a name.

Two things stay manual. Someone makes the repository before its name goes into the list, because Terraform cannot import a repository that does not exist. Someone also keeps the lookup working, because it calls the GitHub API on every plan.


Part 3 of a series on Terraform state surgery without downtime. Next: Key Vault imports want the versioned URL.

terraformgovernancetutorial

read next