Terraform import to show possible code update

According to a Terraform course I’m going through, as of version 0.13, there is no way for Terraform to update the config files if the resources were modified manually.

I want to know as of now Terraform v1.5.6, is there any update ?

If I provision an AWS EC2 of r5.xlarge via Terraform and update the EC2 instance via the aws console to i3.8xlarge then can terraform import output a possible config change code so that we can copy-paste the changed config ?

Terraform will detect the changes between what is declared in your statement, and what exists in the real world, and attempt to converge back to the statement. So, if you make changes in the real world, and you actually want those changes, then you need to update your declaration of what you want the real world to be.

IE you need to change the Terraform declaration from r5.xlarge to i3.8xlarge. This is not importing a new resource, but rather converging an existing resources to its desired state. You did a “reverse Terraform” – first changing the world and then telling Terraform about it.

Typically you would first change the code, and then via an apply change the real world.

Not to say that people don’t do what you have done all the time - often you need to experiment quickly and use the console. If you want to make the declared state and actual state converge, you can do a terraform plan and see what Terraform proposes to change.

From the result of that plan, you can propagate the changes represented by the actual state into the declared state.

1 Like

In addition to the above, which is correct, in more recent releases of Terraform (not v0.13, I think) you can ask Terraform to produce a “refresh-only plan” which will report on any changes the providers are able to detect were made outside of Terraform:

terraform plan -refresh-only

You could potentially treat the result as a list of instructions for how to change your configuration to make it match the current remote system.

Note though that this sort of thing is “best effort” from Terraform’s perspective. Terraform’s model assumes that the desired state described by the configuration is the source of record and that any object managed by Terraform will be changed exclusively by Terraform. It can often detect simple changes such as to an attribute of an existing object, but more elaborate changes such as adding or removing entire objects can’t always be detected reliably. For best results you should consider each object to either be managed by Terraform or not and be consistent; mapping the configured desired state onto the remote system is a lossy operation – the remote system can’t store all of the metadata Terraform tracks to do its work – so it’s not feasible to reliably reverse that mapping in all cases.