Use cases for import in custom provider

What is the use case for terraform import ?

How should we handle terraform only attribute in case of import ?

We have a custom provider, where we have some terraform only attribute, which doesn’t have a backing api.

So whenever we try to import these, breaking changes appear.

Is there a way to save these attributes from the terraform config file to the state so that it doesn’t show change ?

Is there a way to differentiate import from other operations ?

My understanding of import is, if a user wants to import an existing resource into his configuration.
In that case, the user makes a configuration and try to import it, in that case for terraform only attributes it keeps on showing changes and we have to make hand edits to the state file.

Other is if user lost his state file, so he tries to import the resources. In this case as well, it keeps on showing changes.

Is there a way to to set the terraform only attributes from the config to state file during the import ?

When you say “terraform only attributes” what are you meaning? If there is something that is only stored within the state file when a resource is created that cannot be obtained from the underlying API then it sounds like import isn’t actually possible, so shouldn’t be implemented for that resource. Alternatively if it is something simple and non-secret you could require the value to be included during the import (for example the AWS Route53 record import requires you to format the import string with various IDs, values, etc. in a particular way.

@stuart-c Thank you for replying.

Yeah, by terraform only attributes I mean that once resource is created, there is no way to retrieve those values back if we lose the state file.

Its a content of a file/long string, which contains some meta data.

If we make the id complex then the drawback is we have to set the id the same way, isn’t it ?

Can we pass some extra variables with import ? Or set some attributes from the config file when imported ?

eg. if I have a resource like this

resource "script_log" "ts" {
  name = "run-scripts"  
  tags   = ["t1", "t2"]
  script = file("db-data.txt")

Here script is a terraform only attribute which the api doesn’t return due to some limitations and is non patch, so we have set it as ForceNew true.

Now on create, it does gets saved into state file and any change results in a new resource creation and script_log gets replaced.

On import this is empty(null) and it shows a ForceNew on plan.

And the workaround I am looking for is to save the script from my config to state on import. So if the user loses his state file, on import the value gets copied like the time of creation. And on subsequent change it shows a diff and a forcenew(replacement)