Terraform modularization delete existing infra

Solution 1:

Since you decided to split your infrastructure code in several modules, terraform will treat your resources as a new ones, because their location did change.

Moving resource blocks from one module into several child modules causes Terraform to see the new location as an entirely different resource.

Documentation: https://www.terraform.io/language/modules/syntax#transferring-resource-state-into-modules

There're several ways you can proceed now:

a. You can use refactoring feature of Terraform (available from version 1.1): https://www.terraform.io/language/modules/develop/refactoring and utilize moved block to map old resources to the new ones.

b. You can start with the clean terraform state and manually import resources from your actual infrastructure to the state (https://www.terraform.io/cli/import) (you need to do it for all 28 resources)

But if its your new project, the easiest way would be to just recreate resources from scratch (of course if it's not the production environment containing important data)