Skip to content

Instantly share code, notes, and snippets.

@rostrovsky
Created November 9, 2023 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rostrovsky/fe9177ffc9f30fea6eaa211d2e318a0a to your computer and use it in GitHub Desktop.
Save rostrovsky/fe9177ffc9f30fea6eaa211d2e318a0a to your computer and use it in GitHub Desktop.

Question

i would like to use terragrunt. what i dont like in it is this structure it enforces:

└── live
    ├── prod
    │   ├── app
    │   │   └── main.tf
    │   ├── mysql
    │   │   └── main.tf
    │   └── vpc
    │       └── main.tf
    ├── qa
    │   ├── app
    │   │   └── main.tf
    │   ├── mysql
    │   │   └── main.tf
    │   └── vpc
    │       └── main.tf
    └── stage
        ├── app
        │   └── main.tf
        ├── mysql
        │   └── main.tf
        └── vpc
            └── main.tf

i would rather prefer

└── live
    ├── app
    │   ├── qa
    │   │   └── main.tf
    │   ├── prod
    │   │   └── main.tf
    │   └── stage
    │       └── main.tf
    ├── mysql
    │   ├── qa
    │   │   └── main.tf
    │   ├── prod
    │   │   └── main.tf
    │   └── stage
    │       └── main.tf
    └── vpc
        ├── qa
        │   └── main.tf
        ├── prod
        │   └── main.tf
        └── stage
            └── main.tf

is this possible?

Answer

Yes, it is possible to structure your Terraform and Terragrunt configurations in the way you prefer. Terragrunt is quite flexible in terms of directory structure, and it mostly relies on the terragrunt.hcl file to understand the configuration context. The directory structure you prefer is actually more aligned with organizing resources by type and then by environment, which is a valid approach.

To set up your directory structure as you described, you would need to ensure that each main.tf and its corresponding terragrunt.hcl file are correctly configured. Here is how you can do it:

  1. Directory Structure: Organize your directories as per your preference:

    └── live
        ├── app
        │   ├── qa
        │   │   └── terragrunt.hcl
        │   ├── prod
        │   │   └── terragrunt.hcl
        │   └── stage
        │       └── terragrunt.hcl
        ├── mysql
        │   ├── qa
        │   │   └── terragrunt.hcl
        │   ├── prod
        │   │   └── terragrunt.hcl
        │   └── stage
        │       └── terragrunt.hcl
        └── vpc
            ├── qa
            │   └── terragrunt.hcl
            ├── prod
            │   └── terragrunt.hcl
            └── stage
                └── terragrunt.hcl
    
  2. Terragrunt Configuration (terragrunt.hcl): In each environment-specific folder (like app/qa), you will have a terragrunt.hcl file. This file will include the Terraform configuration specifics for that particular environment. For instance, the terragrunt.hcl in live/app/qa might look something like this:

    terraform {
      source = "../../../modules/app"
    }
    
    inputs = {
      env = "qa"
      // other environment-specific variables
    }

    This configuration tells Terragrunt to use the Terraform code in ../../../modules/app (assuming you have a separate modules directory at the root) and sets some environment-specific variables.

  3. Maintain Your Modules: You should have a separate directory for your Terraform modules. This is where you’ll define the resources that you want to manage with Terraform.

    ├── modules
    │   ├── app
    │   │   └── main.tf
    │   ├── mysql
    │   │   └── main.tf
    │   └── vpc
    │       └── main.tf
    
  4. Deployment: When you run Terragrunt in, for example, live/app/qa, it will use the configuration from the terragrunt.hcl file in that directory, applying the Terraform code from the modules/app directory with the specific inputs for the QA environment.

Remember, the key to using Terragrunt effectively is understanding how terragrunt.hcl files inherit and override settings from each other. You can have a root terragrunt.hcl with common settings and then override or supplement these settings in environment-specific terragrunt.hcl files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment