Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration for import target does not exist error with for_each in import block

I have existing infrastructure that I want to manage with terraform and import into the statefile. In this case I am trying to import storage containers. The storage account itself has already been imported into the statefile.

In my main.tf I have:

import {
  for_each = toset(var.storage_containers)

  to = module.storage_container.azurerm_storage_container.storage_container[each.key]
  id = "https://${module.storage_account.name}.blob.core.windows.net/${each.key}"
}

module "storage_container" {
  source = "../../../modules/storage/storage_container"

  for_each = toset(var.storage_containers)

  storage_container = {
    name        = each.key
    access_type = "Private"
  }
  storage_account_name = module.storage_account.name
}

In variables.tf I have the storage_containers variable set with a default value, which contains the storage container names.

variable "storage_containers" {
  description = "List of storage containers"
  type = list(string)
  default = [
    "artifact",
    "fire",
    "models",
    "security",
    "snapshots",
    "statistics",
  ]
}

When I run terraform plan I get the following error:

Planning failed. Terraform encountered an error while generating this plan.

╷
│ Error: Configuration for import target does not exist
│ 
│ The configuration for the given import
│ module.storage_container.azurerm_storage_container.storage_container["models"]
│ does not exist. All target instances must have an associated configuration
│ to be imported.
╵

When I rerun it the error can change by mentioning another storage container in the error message. The containers exist for sure (double checked). I can't figure out where I am going wrong on this.

I use terraform version 1.7.1 and azurerm provider version 3.89.0.

I have tried running in locally but also via an azure devops pipeline and both give the same error. I also tried an earlier version of terraform 1.7.0.

My expectations would of course be that the storage containers are imported into the state file.

like image 271
bramvdk Avatar asked Oct 30 '25 06:10

bramvdk


1 Answers

The import block should be:

import {
  for_each = toset(var.storage_containers)

  to = module.storage_container[each.key].azurerm_storage_container.storage_container
  id = "https://${module.storage_account.name}.blob.core.windows.net/${each.key}"
}
like image 75
bramvdk Avatar answered Nov 02 '25 21:11

bramvdk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!