Skip to content

Plannable Import: Support for_each #33624

@slushysnowman

Description

@slushysnowman

Terraform Version

Terraform v1.5.4
on linux_amd64

Use Cases

The ability to use for_each within an import block would greatly improve the use cases where the import block can be applied.

In complex scenarios, where you are importing resources in a variety of environments, to resources that use for_each, the current way that the import block can be used is very limited. You would basically need to define separate import blocks for every resource in every environment, which becomes so complex that it verges on being totally unusable.

In our use case in this scenario we use the traditional terraform import command and script the import outside of the Terraform plan. This works, but adds 'pre' steps that need to be done before a plan can be safely applied in migration scenarios, which adds complexity, as well as involving completely custom code that brings its own risks.

A logical extension of the functionality of the import block would be to add support for for_each. Then, in combination with the support for interpolation which was recently merged, this would potentially allow you to use the import block from the same 'source of truth' as you use to define the resources you are importing to.

This would greatly reduce the complexity of complex migration scenarios.

An example of this that I wrote on a different issue is:
Say you're managing 10 kubernetes clusters and are in the process of migrating to use Terraform to configure them.

Each cluster can have varying numbers of namespaces, with different names.

If you're planning to define the namespaces via a for_each based on some sort of logic (some values file, or variables or whatever), in this migration scenario it makes life so much easier if you can also define a for_each on the import block.

The alternative in the current scenario would be something like defining 10x import.tf files containing all the static import blocks for each cluster, and doing it that way, which is quite a painful process.

Attempted Solutions

Something along these lines:

import {
  for_each = var.namespaces
  to = kubernetes_namespace.main[each.key]
  id = each.value.name
}

resource "kubernetes_namespace" "main" {
  for_each = var.namespaces
  metadata {
    annotations = {
      name = each.value.name
    }
    labels = each.value.labels
    name = each.value.name
  }
}

This does not work due to the lack of for_each support in the import block

Proposal

Support the for_each argument in the import block to make something like the below possible:

import {
  for_each = var.namespaces
  to = kubernetes_namespace.main[each.key]
  id = each.value.name
}

resource "kubernetes_namespace" "main" {
  for_each = var.namespaces
  metadata {
    annotations = {
      name = each.value.name
    }
    labels = each.value.labels
    name = each.value.name
  }
}

References

Requests for for_each support have been mentioned in (at least):

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions