0
0
Terraformcloud~30 mins

Terragrunt for DRY configurations in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terragrunt for DRY configurations
📖 Scenario: You are managing infrastructure for a company that has multiple environments: development, staging, and production. Each environment uses similar Terraform modules but with different settings. To avoid repeating the same configuration in each environment, you want to use Terragrunt to keep your Terraform code DRY (Don't Repeat Yourself).
🎯 Goal: Build a Terragrunt configuration that uses a common Terraform module with shared settings, and environment-specific overrides, to manage infrastructure for three environments without repeating code.
📋 What You'll Learn
Create a Terragrunt root configuration with common settings
Create environment-specific Terragrunt configurations that inherit from the root
Use the terraform block to specify the source module
Use the inputs block to pass variables to the Terraform module
Demonstrate how to keep configurations DRY using Terragrunt's include feature
💡 Why This Matters
🌍 Real World
Companies often manage multiple environments with similar infrastructure. Terragrunt helps avoid repeating code by sharing common configuration and allowing environment-specific overrides.
💼 Career
Understanding Terragrunt is valuable for cloud engineers and DevOps professionals who manage infrastructure as code efficiently and maintainably.
Progress0 / 4 steps
1
Create the root Terragrunt configuration
Create a file named terragrunt.hcl in the root folder with a terraform block that sets the source to git::https://github.com/example/infrastructure-modules.git//network. Also, add an inputs block with region = "us-east-1" and environment = "root".
Terraform
Need a hint?

Use the terraform block to specify the module source URL. Then add an inputs block with the required variables.

2
Create the development environment Terragrunt configuration
Create a file named dev/terragrunt.hcl that uses the include block to inherit from the root terragrunt.hcl. Override the inputs block to set environment = "dev" and add instance_count = 1.
Terraform
Need a hint?

Use the include block with find_in_parent_folders() to inherit root settings. Then override inputs with environment-specific values.

3
Create the staging environment Terragrunt configuration
Create a file named staging/terragrunt.hcl that includes the root configuration using include. Override the inputs block to set environment = "staging" and instance_count = 2.
Terraform
Need a hint?

Similar to the dev environment, use include and override inputs for staging.

4
Create the production environment Terragrunt configuration
Create a file named prod/terragrunt.hcl that includes the root configuration using include. Override the inputs block to set environment = "prod" and instance_count = 3. This completes the DRY Terragrunt setup for all environments.
Terraform
Need a hint?

Follow the same pattern as previous environments to complete the production configuration.