DEV Community

Bartłomiej Danek
Bartłomiej Danek

Posted on • Originally published at bard.sh

Terragrunt folder structure and include

folder structure

Terragrunt projects split config into two layers: a root hcl file with shared settings, and per-unit terragrunt.hcl files that inherit from it.

root.hcl
environments/
├── dev/
│   ├── vpc/
│   │   └── terragrunt.hcl
│   ├── rds/
│   │   └── terragrunt.hcl
│   └── eks/
│       └── terragrunt.hcl
└── prod/
    ├── vpc/
    │   └── terragrunt.hcl
    ├── rds/
    │   └── terragrunt.hcl
    └── eks/
        └── terragrunt.hcl
Enter fullscreen mode Exit fullscreen mode

Each leaf terragrunt.hcl is one Terraform unit - its own state file, its own apply.

include - inherit from root

# environments/prod/vpc/terragrunt.hcl
include "root" {
  path = find_in_parent_folders("root.hcl")
}

terraform {
  source = "git::https://github.com/org/tf-modules.git//vpc?ref=v1.2.0"
}

inputs = {
  name       = "prod-vpc"
  cidr_block = "10.0.0.0/16"
}
Enter fullscreen mode Exit fullscreen mode

root.hcl

# root.hcl
locals {
  env    = basename(dirname(get_terragrunt_dir()))
  region = "eu-west-1"
}

remote_state {
  backend = "s3"
  config = {
    bucket         = "my-tf-state"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = local.region
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite_terragrunt"
  }
}

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "aws" {
  region = "${local.region}"
}
EOF
}
Enter fullscreen mode Exit fullscreen mode

find_in_parent_folders

Walks up the directory tree until it finds the file - no hardcoded paths needed.

include "root" {
  path = find_in_parent_folders()          # finds root.hcl by default
}

include "root" {
  path = find_in_parent_folders("root.hcl") # explicit name
}
Enter fullscreen mode Exit fullscreen mode

Originally published at https://bard.sh/posts/terragrunt_folder_structure/

Top comments (0)