Some of our new APIs require one‑to‑many migrations (from older deprecated APIs) that Terraform’s built‑in moved block can’t automate (as it only handles 1:1).
Because moved only supports a single target resource, we must use terraform import for the extra assignments. Unfortunately, import blocks only work at the root module level (see Allow import blocks within child modules · Issue #33474 · hashicorp/terraform · GitHub), so platform teams can’t embed them in reusable modules—every downstream user must write their own import steps, resulting in a poor UX at scale.
Proposed solution
Instead of relying on root‑level import, we’d like to make the create operation idempotent when a resource already exists. Concretely:
- Introduce an opt‑in flag (e.g.
imports_if_exists) on our new resources. - If set, a
CREATEof an already‑existing resource would not error, but instead fetch its state via a GET and adopt it into state.
This lets modules ship self‑contained migrations without requiring root imports.
This lets modules ship self‑contained migrations without requiring root imports.
Questions:
- Is adding this opt‑in idempotent‑create behavior compatible with Terraform’s design principles & best practices?
- Are there any core implications or edge cases we haven’t considered?
- Do any existing providers implement a similar “create if exists” pattern for resources?
- Would you recommend another supported mechanism to solve multi‑resource migrations without root‑level imports?