Bird
Raised Fist0
Terraformcloud~20 mins

Terragrunt for DRY configurations in Terraform - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Terragrunt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Terragrunt's Purpose

What is the main benefit of using Terragrunt with Terraform?

AIt helps avoid repeating the same Terraform code by managing configurations centrally.
BIt automatically scales cloud resources based on traffic.
CIt replaces Terraform and runs cloud infrastructure without code.
DIt provides a graphical interface to design cloud infrastructure visually.
Attempts:
2 left
💡 Hint

Think about how Terragrunt helps when you have many similar infrastructure setups.

Configuration
intermediate
2:00remaining
Terragrunt Configuration Inheritance

Given a Terragrunt configuration file terragrunt.hcl that includes a remote_state block, which option correctly shows how to inherit this remote state configuration in a child module?

Terraform
remote_state {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "global/terraform.tfstate"
    region = "us-east-1"
  }
}
A
include {
  path = find_in_parent_folders()
}

remote_state = include.get().remote_state
B
include {
  path = find_in_parent_folders()
}

remote_state {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "global/terraform.tfstate"
    region = "us-east-1"
  }
}
C
include {
  path = find_in_parent_folders()
}

remote_state = local.remote_state
D
remote_state {
  backend = "s3"
  config = {
    bucket = "my-terraform-state"
    key    = "global/terraform.tfstate"
    region = "us-east-1"
  }
}
Attempts:
2 left
💡 Hint

Look for the syntax that references the parent's remote_state block after including it.

Architecture
advanced
2:00remaining
Terragrunt Folder Structure for Multiple Environments

You want to manage Terraform infrastructure for two environments: dev and prod. Using Terragrunt, which folder structure best supports DRY principles and environment isolation?

A
/terraform/
  /dev/
    main.tf
  /prod/
    main.tf
  terragrunt.hcl
B
/modules/
  /dev/
    main.tf
  /prod/
    main.tf
C
/live/
  /dev/
    terragrunt.hcl
  /prod/
    terragrunt.hcl
  terragrunt.hcl
D
/live/
  /dev/
    main.tf
  /prod/
    main.tf
Attempts:
2 left
💡 Hint

Think about where Terragrunt files should be placed to share common configs and separate environments.

security
advanced
2:00remaining
Securing Sensitive Variables in Terragrunt

Which approach best protects sensitive variables like passwords when using Terragrunt with Terraform?

AStore sensitive variables directly in <code>terragrunt.hcl</code> files in plain text.
BUse environment variables or encrypted secrets managers and reference them in Terragrunt configs.
CCommit sensitive variables in Terraform <code>.tf</code> files to version control for easy access.
DHardcode sensitive values in Terraform modules to avoid passing them through Terragrunt.
Attempts:
2 left
💡 Hint

Think about how to keep secrets safe and avoid exposing them in code repositories.

service_behavior
expert
2:00remaining
Terragrunt Behavior with Dependency Blocks

Consider this Terragrunt configuration snippet:

dependency "vpc" {
  config_path = "../vpc"
}

inputs = {
  vpc_id = dependency.vpc.outputs.vpc_id
}

What happens if the ../vpc module has not been applied yet when you run Terragrunt in this module?

ATerragrunt ignores the dependency and applies the current module without the <code>vpc_id</code> input.
BTerragrunt fails with an error because the dependency outputs are missing.
CTerragrunt prompts the user to manually apply the dependency module before continuing.
DTerragrunt automatically applies the <code>../vpc</code> module first, then uses its outputs.
Attempts:
2 left
💡 Hint

Think about how Terragrunt manages dependencies between modules automatically.

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

  1. 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.
  2. Step 2: Compare options

    Options B, C, and D describe incorrect uses or misunderstandings of Terragrunt's purpose.
  3. Final Answer:

    To reuse Terraform configurations and avoid repeating code -> Option A
  4. Quick Check:

    Terragrunt = DRY Terraform code reuse [OK]
Hint: Terragrunt helps avoid repeating Terraform code [OK]
Common Mistakes:
  • Thinking Terragrunt replaces Terraform
  • Believing Terragrunt changes Terraform language
  • Confusing Terragrunt with application deployment tools
2. Which of the following is the correct way to include a Terraform module using Terragrunt?
easy
A. module "app" { source = "./app" }
B. include { path = find_in_parent_folders() }
C. terraform { backend = "s3" }
D. resource "aws_instance" "web" {}

Solution

  1. Step 1: Identify Terragrunt syntax for including configs

    Terragrunt uses the include block with path = find_in_parent_folders() to reuse parent configs.
  2. Step 2: Differentiate from Terraform syntax

    Options A, C, and D are Terraform syntax: A is a module block, C is backend config, D is a resource block, not Terragrunt includes.
  3. Final Answer:

    include { path = find_in_parent_folders() } -> Option B
  4. Quick Check:

    Terragrunt include uses find_in_parent_folders() [OK]
Hint: Terragrunt includes parent config with include + find_in_parent_folders() [OK]
Common Mistakes:
  • Using Terraform module syntax instead of Terragrunt include
  • Confusing backend config with include
  • Writing resource blocks inside Terragrunt files
3. Given this Terragrunt configuration snippet:
include {
  path = find_in_parent_folders()
}

inputs = {
  region = "us-east-1"
  env    = "prod"
}

What will happen when you run terragrunt apply in this folder?
medium
A. Terragrunt will apply Terraform code without any variables
B. Terragrunt will fail because inputs cannot be used with include
C. Terragrunt will ignore the inputs block and only use parent config
D. Terragrunt will apply Terraform code using the parent config and inputs region=us-east-1, env=prod

Solution

  1. Step 1: Understand include and inputs usage

    The include block imports parent config. The inputs block adds or overrides variables for this folder.
  2. Step 2: Predict Terragrunt behavior on apply

    Terragrunt merges parent config with local inputs, so region and env variables are set as given.
  3. Final Answer:

    Terragrunt will apply Terraform code using the parent config and inputs region=us-east-1, env=prod -> Option D
  4. Quick Check:

    Include + inputs = merged config applied [OK]
Hint: Inputs override or add variables when using include [OK]
Common Mistakes:
  • Thinking inputs are ignored with include
  • Assuming inputs cause errors
  • Believing variables are not passed to Terraform
4. You wrote this Terragrunt config:
include {
  path = find_in_parent_folders()
}

inputs = {
  region = "us-west-2"
  env = "dev"
}

terraform {
  source = "../modules/app"
}

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

  1. 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.
  2. Step 2: Identify error cause

    The error "Unsupported block type" usually means the block is misplaced or invalid in Terragrunt config.
  3. Final Answer:

    The terraform block cannot be nested inside the inputs block -> Option A
  4. 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

  1. Step 1: Understand DRY with Terragrunt

    Terragrunt allows sharing common config via a root config and include blocks in child folders.
  2. 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.
  3. Final Answer:

    Create a root Terragrunt config with backend settings and use include in each environment folder -> Option C
  4. Quick Check:

    Root config + include = DRY backend config [OK]
Hint: Put shared backend in root config, include it in environments [OK]
Common Mistakes:
  • Copying backend config to each environment
  • Configuring backend only in Terraform modules
  • Using different backends unnecessarily