Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Follow the same pattern as previous environments to complete the production configuration.
Practice
(1/5)
1. What is the main purpose of using Terragrunt with Terraform?
easy
A. To reuse Terraform configurations and avoid repeating code
B. To replace Terraform with a new tool
C. To write Terraform code in a different programming language
D. To deploy applications without infrastructure
Solution
Step 1: Understand Terragrunt's role
Terragrunt is designed to help reuse and share Terraform code, making it easier to manage infrastructure without repeating code.
Step 2: Compare options
Options B, C, and D describe incorrect uses or misunderstandings of Terragrunt's purpose.
Final Answer:
To reuse Terraform configurations and avoid repeating code -> Option A
When running terragrunt apply, you get an error: "Error: Unsupported block type". What is the likely cause?
medium
A. The terraform block cannot be nested inside the inputs block
B. The inputs block must come before include
C. The source path is incorrect
D. The region variable is invalid
Solution
Step 1: Check Terragrunt config block usage
Terragrunt requires the terraform { source = ... } block at the root level. It cannot be nested inside other blocks like inputs.
Step 2: Identify error cause
The error "Unsupported block type" usually means the block is misplaced or invalid in Terragrunt config.
Final Answer:
The terraform block cannot be nested inside the inputs block -> Option A
Quick Check:
Misplaced terraform block causes error [OK]
Hint: Terraform block must be correctly placed in Terragrunt config [OK]
Common Mistakes:
Placing terraform block inside inputs or include
Wrong order of blocks causing syntax errors
Incorrect source path causing unrelated errors
5. You manage multiple environments (dev, staging, prod) with Terragrunt. You want to avoid repeating the backend configuration for each environment. Which approach best follows DRY principles?
hard
A. Define backend only in Terraform modules, not in Terragrunt
B. Copy the backend block into each environment's Terragrunt config
C. Create a root Terragrunt config with backend settings and use include in each environment folder
D. Use different backend types for each environment
Solution
Step 1: Understand DRY with Terragrunt
Terragrunt allows sharing common config via a root config and include blocks in child folders.
Step 2: Evaluate options for backend reuse
Create a root Terragrunt config with backend settings and use include in each environment folder uses root config for backend, avoiding repetition. Copy the backend block into each environment's Terragrunt config repeats code, violating DRY. Define backend only in Terraform modules, not in Terragrunt is incorrect because backend is configured in Terragrunt for remote state. Use different backend types for each environment adds complexity without reuse.
Final Answer:
Create a root Terragrunt config with backend settings and use include in each environment folder -> Option C
Quick Check:
Root config + include = DRY backend config [OK]
Hint: Put shared backend in root config, include it in environments [OK]