Terragrunt for Beginners

Terragrunt Blocks

Terragrunt Blocks Overview

In this lesson, we’ll dive into the seven core Terragrunt configuration blocks that power your Terraform workflows. Whether you’re already familiar with Terraform or new to Terragrunt’s advanced features, you’ll learn how each block streamlines state management, dependency handling, and DRY (Don’t Repeat Yourself) principles for infrastructure as code.

Note

Terragrunt extends Terraform by adding useful wrappers around remote state, dependencies, and configuration generation. If you’re new to Terragrunt, check out the official Terragrunt documentation.

Summary of Terragrunt Blocks

BlockPurposeKey Benefit
terraformPass extra arguments directly to the Terraform CLI.Fine-tune your Terraform runs.
remote_stateConfigure reading from and writing to remote state backends.Centralize and secure state storage.
includeImport and merge settings from another Terragrunt config file.Share common configurations.
localsDefine reusable values and expressions.Keep code DRY and maintainable.
dependencyDeclare a single dependency on another Terragrunt module.Expose outputs from one module.
dependenciesList multiple module dependencies for orchestration.Orchestrate complex multi-module runs.
generateAuto-generate additional HCL or JSON files before Terraform execution.Automate boilerplate file creation.

1. terraform Block

Use the terraform block to pass custom flags and settings to the Terraform CLI:

terraform {
  extra_arguments "plan_args" {
    commands = ["plan"]
    arguments = ["-var-file=prod.tfvars", "-parallelism=10"]
  }
}

2. remote_state Block

Configure your remote backend once in Terragrunt to avoid repeating it in every module:

remote_state {
  backend = "s3"
  config = {
    bucket         = "my-terraform-states"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "us-east-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

Warning

Always enable state locking (e.g., DynamoDB for S3 backends) to prevent concurrent state modifications.

3. include Block

Share common settings by importing another Terragrunt file:

include {
  path = find_in_parent_folders("terragrunt.hcl")
}

4. locals Block

Define and reuse variables and expressions:

locals {
  environment = "production"
  tags = {
    Project = "website"
    Env     = local.environment
  }
}

5. dependency Block

Declare a single dependency to retrieve outputs from another module:

dependency "vpc" {
  config_path = "../vpc"
}
# Use the output
resource "aws_subnet" "private" {
  vpc_id = dependency.vpc.outputs.vpc_id
}

6. dependencies Block

Orchestrate multiple modules by listing them all:

dependencies {
  paths = ["../vpc", "../security-group", "../database"]
}

When you run terragrunt apply-all, it ensures each module runs in the correct order.

7. generate Block

Automatically generate additional configuration files (HCL or JSON):

generate "backend_tf" {
  path      = "backend.tf"
  if_exists = "overwrite"
  contents  = <<EOF
terraform {
  backend "s3" {
    bucket = "generated-backend"
    key    = "generated.tfstate"
    region = "us-east-1"
  }
}
EOF
}

Next Steps

  • Explore Terragrunt Remote State for advanced backend options.
  • Learn how to combine dependency and dependencies in a real-world multi-module project.
  • Check out Terraform CLI Docs for all available commands and flags.

Watch Video

Watch video content

Previous
Other Terragrunt Functions