Terraform provides simple & very easy way to manage your cloud using uniform & simple scripts across different clouds. Terraform can manage existing and popular service providers as well as custom in-house solutions. Key features of Terraform are

  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation

Terraform requires & generates 3 types of files

  • tf files: Is the main configuration file which is used for describing the components needed to run a single application or your entire datacenter.
  • state file: Terraform stores various resource metadata to actual resource IDs so that Terraform knows what it is managing.
  • tfvars file: This is the least important file & having this makes limited hard codings in your tf files.

It is very important to structure your terraform files in a specific order to achieve modularity & to main a clean structure.

Modular folder structure

More and more you start using terraform the number of tf files starts increasing & it gets very difficult to maintain it. On the commonly used structure is to separate the modules files into separate folder called module. Incase you have multiple modules & if these mudules are by itself having multiple files is it better to have resource specific files under the modules directory. Refer module files in your main.tf as mentioned below

module "site" {
   source  = "../../modules/site"

Place your tfvars file in a separate folder. This helps in maintaining multiple tfvars file for different deployments. In most of the cases same modules with different set of vars & main.tf (bootstrapper tf) used to create different kinds of deployments (Small, Medium, Large)

Place your states file in separate folders or remote repositories. These state files can be named to reflect different deployments. In most of the cases the vars differs in inputting no of instances, vpc cidr, region name etc

Finally maintain a separate folder for placing your bootstrap file main.tf. The larger your infra becomes it becomes necessary to create maintain and test the infra by having a small mockup for testing. This will require less no of resource & some alteration in the bootstrapper file. The bootstrapper file is the one which will invoke the necessary module files. There also other instances where you might want to maintain a staging setup with different combination of resources.