Best Practices for Terraform Project Structure (Edited)

Before, I managed all the infrastructure of my Terraform project by writing all the resources in a single main.tf file.

Now I want to split the terraform configuration by distributing it from one directory to several subfolders, for example:

		
terraform-project/ β”œβ”€β”€ network/ β”‚ Β  β”œβ”€β”€ main.tf β”‚ Β  └── variables.tf β”œβ”€β”€ compute/ β”‚ Β  β”œβ”€β”€ main.tf β”‚ Β  └── variables.tf β”œβ”€β”€ main.tf └── terraform.tfvars

The problem occurs when I use terraform command:

		
terraform apply network

Terraform creates a plan that will remove resources defined outside the network folder. How can I organize the repository so that changes affect only the needed group of resources and the current state is not broken?
Please tell me about best practices.

Andreas Cook

5 months ago

10 answers

60 views

Rating

00
Answer

Answers

Kristina Butters

5 months ago

Rating

00

You're gonna think I'm a bore, but I highly recommend exploring the official terraform documentation. It provides a thorough introduction to getting started with Terraform and offers detailed guidance on structuring your terraform projectβ€”from basic simple structure to full production environments.

Reply

Mika Arias

5 months ago

1 comment

Rating

00

Sometimes using modules makes things too complex. Consider using Terragrunt.Β 

Terragrunt is a tool for improving Terraform projects and managing infrastructure. I’ve seen that it makes working on big projects much easier.

It helps you manage your state in one place. You can set up remote storage like S3 or GCS with little hassle, which reduces conflicts and makes everything more reliable.

It also follows the idea of DRY (Don’t Repeat Yourself). This means you can put common settings into one template, so you don’t need to write the same Terraform code over and over. This really speeds up the deployment process.

Another cool thing is that it makes working with variables and configurations flexible. You can easily change settings for different environments like development, staging, or production, which makes scaling your project a lot simpler.

And lastly, it helps keep your code organized. With Terragrunt, even complex repositories become easier to understand and manage.

Reply

    Andreas Cook

    5 months ago

    1 comment

    Rating

    00

    Bro, this looks like an ad ;)

    Reply

      Mika Arias

      5 months ago

      1 comment

      Rating

      00

      Yeah, in a way. I like this tool. It helps me working with terraform and configuration management, and I recommend it.

      Reply

        Andreas Cook

        5 months ago

        1 comment

        Rating

        00

        I'm preparing to launch my Terraform workspaces in AWS. What challenges might I encounter when using Terragrunt, especially concerning the management of multiple accounts, scalability, configuration errors, and setup complexity?

        Reply

          Mika Arias

          5 months ago

          1 comment

          Rating

          00

          I've been using Terraform with Terragrunt on Amazon Web Services (AWS) and haven't experienced any issues that are specific to the platform.Β 

          However, it's worth noting that there are some inherent nuances in using Terragrunt.

          For example, if you need to manage multiple accounts, the issue of properly configuring roles and permissions immediately arises. If everything isn't set up perfectly, you may encounter problems when working with remote state (like S3 and DynamoDB). It's crucial to establish proper trust relationships between accounts to ensure everything works as intended.

          Another challenge, especially if you have a large Terraform project structure, is scaling. Terragrunt allows for inheriting configurations, which may seem convenient at first, but when you have many environments, you end up with a very tangled file hierarchy. If there's an error in the base configuration, it might "leak" into several workspaces at once, making it hard to pinpoint the problem.

          It's also worth noting that setting up Terragrunt itself requires extra effort. For someone accustomed to standard Terraform, the directory structure and inheritance logic can seem complicated. Initially, you end up spending a lot of time parsing through the Terragrunt and Terraform documentation and setting things up properly so you don't have to fix a bunch of minor issues later on.

          Overall, Terragrunt is a powerful tool, but it demands precision and careful attention, especially when working in AWS with multiple accounts and multiple environments. If you properly configure permissions, maintain a clear structure within your Terraform configuration, and prepare for the nuances of inheritance, you can work quite effectively with it.

          Reply

          Andreas Cook

          5 months ago

          Rating

          00

          I don't think I need Terragrunt yet, but thanks anyway. If my project grows, I'll be upfront about this tool.

          Reply

Adam Kowalski

5 months ago

1 comment

Rating

00

Just moving files into subfolders is not enough because Terraform only works with files in the current folder. The key solution is to change your code into modules.

In your case, you can create a separate folder for modules so that the project structure looks like this:

		
terraform-project/ β”œβ”€β”€ modules/ β”‚ Β  β”œβ”€β”€ network/ β”‚ Β  β”‚ Β  β”œβ”€β”€ main.tf β”‚ Β  β”‚ Β  └── variables.tf β”‚ Β  └── compute/ β”‚ Β  Β  Β  β”œβ”€β”€ main.tf β”‚ Β  Β  Β  └── variables.tf β”œβ”€β”€ main.tf └── terraform.tfvars

Then, in the root main.tf, include these modules with module blocks. This approach helps you manage the state in one place and avoids unwanted changes when applying changes in separate modules.

Reply

    Ola Richardson

    5 months ago

    Rating

    00

    I can add that if you already have your infrastructure deployed, you can move it to a new modular structure without breaking the existing resources. Use the command:

    		
    terraform state mv

    This command lets you carefully move resources to a new location in the Terraform state. It helps keep the infrastructure working while you reorganize your Terraform configuration.

    Reply

Sandro Sandoval

5 months ago

Rating

00

By default, Terraform only reads files in the current Terraform directory. Moving part of the configurationΒ into subfolder without declaring it a Terraform module may cause Terraform to not see some of the code and try to delete the associated resources. Always set up the different sections as Terraform modules and include them with module blocks in your main Terraform file. This way, all resources stay in one state.

Reply