Import Resource and Required Values

Hey friends,

I don’t know if I’ve misunderstood something about the Plugin Framework or if this is a new feature request.

I am creating a new resource with two required fields: label and cluster_id.

I have implemented ImportState which is working correctly:

main.tf:

resource "ncm_application" "ctetest" {}

Command:

terraform import ncm_application.ctetest 4248

However, when I run terraform plan or terraform apply, I receive the errors:

The argument "cluster_id" is required, but no definition was found.
The argument "label" is required, but no definition was found.

So my question is, do I need to mark these as Optional: true in the schema but reject requests to Create that do not have proper values for label and cluster_id? And if so, there is some friction from a Plugin implementer’s standpoint because it seems like I lose essential validation functionality.

Thank you!

I’m confused… If these attributes are supposed to be required, why aren’t you writing the values in here:

before you run the import?

I would assume importing a resource would bring those values in (because you’ve supplied the id in the import). Perhaps that’s a bad assumption?

Ah, right.

Terraform’s import operation does not automatically generate changes to your .tf files nor does it store existing settings in a way which would allow you to not specify fields which otherwise be required.

To use the Terraform import operation you are expected to first write .tf files that fully describe the existing state of the system - and then the terraform import command just tells Terraform “Don’t create this resource, instead take over management of this existing object”.

Following an import or batch of imports, you should always then immediately run a plan, as any discrepancy between what you have written in the .tf files and the actual state of the object(s), is something Terraform will propose to “correct” - at which point you then need to apply human reasoning to decide whether you really want to allow Terraform to apply these “corrections”, or if what you wrote in the .tf files needs to be adapted to match the real state of the objects so that the “corrections” are no longer proposed by Terraform on subsequent plan operations.

1 Like

Thank you. I misunderstood the operation.

FWIW, there are tools able to generate terraform configuration files and state from existing resources, but all of those I know need explicit provider support.

See terraformer for example: GitHub - GoogleCloudPlatform/terraformer: CLI tool to generate terraform files from existing infrastructure (reverse Terraform). Infrastructure to Code