Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct way to setup multiple logically organized sub folders in a terraform repo?

Currently I am working on a infrastructure in azure that comprises of the following:

  • resource group
  • application gateway
  • app service
  • etc

everything I have is in one single main.tf file which I know was a mistake however I wanted to start from there. I am currently trying to move each section into its own sub folder in my repo. Which would look something like this:

terraform-repo/
├── applicationGateway/
│   ├── main.tf
│   ├── vars.tf
├── appService/
│   ├── main.tf
│   └── vars.tf
├── main.tf
└── vars.tfvars

However when I create this while trying to move over from the single file structure I get issues with my remote state where it wants to delete anything that isn't a part of the currently worked on sub folder. For example if I wanted to run terraform apply applicationGateway I will get the following:

  # azurerm_virtual_network.prd_vn will be destroyed

Plan: 0 to add, 2 to change, 9 to destroy.

What is the correct way to setup multiple logically organized sub folders in a terraform repo? Or do I have to destroy my current environment to get it to be setup like this ?

like image 902
Pablo Marti Cordero Avatar asked Feb 03 '20 15:02

Pablo Marti Cordero


People also ask

Does terraform apply subfolders?

There is one module to deploy a frontend-app, another to deploy a backend-app, another for the MySQL database, and so on. To deploy such an environment, you'd have to manually run terraform apply in each of the subfolder, wait for it to complete, and then run terraform apply in the next subfolder.

What should be in main TF?

main.tf should be the primary entrypoint. For a simple module, this may be where all the resources are created. For a complex module, resource creation may be split into multiple files but any nested module calls should be in the main file.


1 Answers

You are seeing this issue because terraform ignores subfolders, so those resources are not being included at all anymore. You would need to configure the subfolders to be Terraform Modules, and then include those modules in your root main.tf

like image 170
Mark B Avatar answered Sep 21 '22 16:09

Mark B