Why does import need my var file?

When running terraform import <resource address> <infrastructure identifier> i am promoted for my var-file. This puzzles me.

Why does terraform need to see my variables file in order to perform my import? Adding resources to the terraform state shouldn’t require any knowledge of my currently-written config. Additionally, from what i’ve experienced so far, terraform also isn’t doing any checks to make sure the resources i’m importing have anything to do with my variables.

What does it matter to terraform if i pass in the var-file or not? Some colleagues have expressed confusion that terraform doesn’t just read the var file to get the resource’s name.

Hi @isaacngym,

Importing requires interaction with the provider, and therefore the configuration must be evaluated in order to ensure that the correct provider is used for the import, and that the provider is correctly configured. This means that the configuration must be complete and valid in order for evaluation to succeed, and since variables can feed into provider configuration, they are always included in that evaluation.

While the import process has minimal interaction with the configuration currently, it’s possible that import may be more configuration-driven in the future, allowing for declarative imports which can be more easily managed and tracked via configuration.