0
0
Terraformcloud~5 mins

Terragrunt for DRY configurations in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Managing multiple Terraform configurations can be repetitive and error-prone. Terragrunt helps by letting you write shared settings once and reuse them, saving time and avoiding mistakes.
When you have many Terraform modules that share common settings like backend or provider configurations.
When you want to keep your Terraform code clean and avoid copying the same blocks in multiple places.
When you need to manage infrastructure across multiple environments like dev, staging, and production with similar setups.
When you want to easily update shared configurations in one place and have changes apply everywhere.
When you want to simplify running Terraform commands across multiple modules with consistent options.
Config File - terragrunt.hcl
terragrunt.hcl
terraform {
  source = "git::https://github.com/example-org/terraform-modules.git//vpc"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  region = "us-east-1"
  environment = "dev"
}

This file tells Terragrunt where to find the Terraform module code with source.

The include block imports shared settings from parent folders, helping avoid repetition.

The inputs block passes variables like region and environment to the Terraform module.

Commands
This command initializes the Terraform working directory using Terragrunt, downloading modules and providers as configured.
Terminal
terragrunt init
Expected OutputExpected
Downloading terraform modules... Initializing the backend... Terraform has been successfully initialized!
This command shows the changes Terragrunt will apply to your infrastructure, helping you review before making changes.
Terminal
terragrunt plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the planned changes automatically without asking for confirmation, creating or updating resources.
Terminal
terragrunt apply -auto-approve
Expected OutputExpected
Applying changes... Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command removes all resources managed by the Terraform module, cleaning up your infrastructure.
Terminal
terragrunt destroy -auto-approve
Expected OutputExpected
Destroy complete! Resources: 1 destroyed.
-auto-approve - Skip manual approval to destroy resources immediately
Key Concept

If you remember nothing else from this pattern, remember: Terragrunt lets you write shared Terraform settings once and reuse them everywhere to keep your code clean and consistent.

Common Mistakes
Copying backend and provider blocks in every Terraform module instead of using Terragrunt includes.
This causes duplication and makes updates slow and error-prone.
Use Terragrunt's include feature to share backend and provider configurations in a single parent file.
Running terraform commands directly instead of using terragrunt commands.
Terraform commands won't apply the shared configurations and inputs managed by Terragrunt.
Always run terraform commands through Terragrunt to ensure consistent configuration and variable passing.
Not organizing Terragrunt files in a folder structure that reflects environments and modules.
This makes it hard to manage different environments and reuse configurations properly.
Structure your folders with separate directories for environments and modules, each with their own terragrunt.hcl that includes shared settings.
Summary
Create a terragrunt.hcl file to define shared Terraform module source and inputs.
Use terragrunt commands like init, plan, apply, and destroy to manage infrastructure with shared configs.
Use the include block in terragrunt.hcl to avoid repeating backend and provider settings.