Importing a resource twice in the same state file

Using terraform import, i am able to import a resource twice in the same state file. Shouldn’t terraform throw an error or a warning that the resource id already exists in the state file?

Hi @rahulrj,

When you use terraform import it is your responsibility to ensure that what you are importing is sensible.

When Terraform is the one responsible for creating an object the provider will typically try to detect if a conflicting object already exists and return an error if so, but when importing existing infrastructure Terraform just imports whatever object you specified, because it has no way to know how those objects were created or where else they might be managed.

Specifically when you are importing you must ensure that:

  • Each remote object is imported to at most one resource address across all of your configurations.
  • No other system is trying to manage any object you imported into Terraform.

@apparentlymart Terraform can walk through the state file and look for entries with the same resource ID right? That way it will know if a duplicate resource id exists in the same state file

The short answer is that Terraform does not know which attributes are unique for particular resource types, nor is there any requirement for resources to even have unique attributes.