hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.41k stars 9.5k forks source link

Terraform Fails to Import Resources that Reference a File when using Remote Execution #28451

Open jd4883 opened 3 years ago

jd4883 commented 3 years ago

This lead me to believe and investigate why the file being refenced worked perfectly for plan/apply but always failed to import. My job uses Terraform Enterprise with all workspaces set to remote_exec. I had done similar projects outside of work without ever encountering this issue. When looking at the data lookup error, it becomes clear that the path referenced relates to the remote execution location and not my local path.

I believe the issue is that somewhere in the logic for how terraform import handles resources that there must be some discrepancy in how remote_exec handles references to a file, as these are a non-issue when a resource does not interact with a file reference.

For the purpose of this issue, I don't think it is relevant but th yaml file we are reading has a set of parameters that relate to an IAM user or role, which I use submodules to parse out the correct values with defaults where items are not set and create standardized IAM roles/users. I can include other code if necessary but I don't think it's going to be helpful to reproduce the issue and recreate it.

Per our account representatives suggestion, I tested with local_exec instead of remomte_exec, which initially failed. If I then take a look at my state entries, and remove the data block referencing the file, and plan/apply again, everything works. From this point on I can import. This tells me that the state file reference is always taking the remote path and does not ever seem to check the local path of any files referenced. I believe this is a bug and not a feature and would really appreciate if this can be fixed. I am able to unblock myself from being able to import resources at this time, but the approach listed above is not the most efficient and ultimately should not be needed.

Please let me know if I can provide any further data points or evidence of this issue and let me know about when a resolution is ready for a test run. I'm a huge fan of terraform, and this issue has caused a lot of pain points in being able to write more sophisticated large scale frameworks that can reliably be scaled up.

jbardin commented 3 years ago

Hi @jd4883

Thanks for filing the issue. I'm not certain what the exact cause of the filature is here without a way to reproduce the issue, but my hunch is that the local environment in which you are running import does not quite match the remote environment. Any arguments to functions like file are essentially considered part of the configuration, so if the value of data.local_file.yaml.filename is not valid in in the local environment, the import may fail.

Since we are working on improvements to both the remote execution model and the import workflow in general, I'm going to tag this to be included in those efforts.

jd4883 commented 3 years ago

Hi @jbardin,

From what I can tell, it seems that remote_exec vs. local_exec is the key component. since the file is read in functionally the same in both cases, but I can do the import without issue when using local_exec, this aligns with what I am seeing.

My proposed solution, is that import does some sort of compare to what is local and what is remote when using remote_exec so that it can correctly calculate what is in a given file. It seems import is only checking what is local and since the path does in fact not match, this errors out.

Please keep me in the loop on what upcoming changes/improvements may be coming as this can be worked around, but is very cumbersome unless doing large bulk operations (we have to convert the remote_exec to local_exec, prune the data block from state relating to the file, and import will work since the data block is re-read in and updated in state.

Cheers,

Jacob