Problem
When using a Terraform version older than 1.7, the terraform import command may report a successful import for a resource managed with for_each, but the resource is not added to the Terraform state file.
Prerequisites
- Terraform version older than
1.7.0.
Cause
This issue is caused by a bug in Terraform versions prior to 1.7.0. When attempting to import a resource instance that is managed by a for_each block but is not yet defined in the configuration, these older versions incorrectly return a success message instead of an error. The state file remains unchanged.
For example, given the following configuration:
variable "my_map" {
type = map(any)
default = {
key1 = "value1"
}
}
resource "random_uuid" "test" {
for_each = var.my_map
}The state only contains the instance for key1.
$ terraform state list random_uuid.test["key1"]
Attempting to import an instance for key2, which is not defined in the my_map variable, incorrectly reports success.
$ terraform import 'random_uuid.test["key2"]' aabbccdd-eeff-0011-2233-445566778899 Import successful! The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.
However, verifying the state shows that the new resource was not added.
$ terraform state list random_uuid.test["key1"]
Solutions
Solution 1: Upgrade Terraform and Update Configuration
The definitive solution is to upgrade your Terraform binary to version 1.7.0 or newer, which contains the fix for this behavior. After upgrading, you must also update your configuration to include the resource you intend to import.
Procedure
- Upgrade your Terraform binary to version
1.7.0or a later version. -
Execute the
terraform importcommand again. You will now receive the correct error message indicating the target is not defined in your configuration.$ terraform import 'random_uuid.test["key2"]' aabbccdd-eeff-0011-2233-445566778899 ╷ │ Error: Configuration for import target does not exist │ │ The configuration for │ the given import random_uuid.test["key2"] does not exist. │ All target instances must have an associated configuration │ to be imported. ╵
-
Modify your configuration to include the resource instance you want to import. In this example, add
key2to themy_mapvariable.variable "my_map" { type = map(any) default = { key1 = "value1" key2 = "value2" } } resource "random_uuid" "test" { for_each = var.my_map } -
Run the
terraform importcommand again. The import will now succeed and correctly add the resource to your state file.$ terraform import 'random_uuid.test["key2"]' aabbccdd-eeff-0011-2233-445566778899 random_uuid.test["key2"]: Importing from ID "aabbccdd-eeff-0011-2233-445566778899"... random_uuid.test["key2"]: Import prepared! Prepared random_uuid for import random_uuid.test["key2"]: Refreshing state... [id=aabbccdd-eeff-0011-2233-445566778899] Import successful! The resources that were imported are shown above. These resources are now in your Terraform state and will henceforth be managed by Terraform.
Outcome
By upgrading to Terraform 1.7.0 or newer, the terraform import command provides accurate feedback, preventing silent failures. After updating your configuration to match the resource being imported, the command successfully adds the resource to the state file as expected.
Additional Information
- The fix for this
importbehavior was introduced in Terraform v1.7.0, and details for this fix are available in the pull request on GitHub.