Terragrunt for Beginners

Terragrunt Blocks

generate Block

The generate block in Terragrunt automates file creation within your working directory. It’s essential for maintaining DRY (Don't Repeat Yourself) Terraform configurations across multiple modules, boosting consistency and simplifying management.

Key Use Cases

  • Centralize provider and backend configurations
  • Enforce version constraints on Terraform providers
  • Generate any auxiliary .tf or other configuration files

Note

Use the generate block to share common Terraform setup and reduce duplication across your modules.

generate Block Attributes

AttributeDescription
nameIdentifier for the generate block, useful when defining multiple blocks.
pathRelative output path for the generated file in the Terragrunt working directory.
if_existsAction if the file already exists. See values in the table below.
comment_prefixPrefix for comments in the generated file (default: #).
disable_signatureWhen true, suppresses the Terragrunt signature comment in the generated file.
contentsThe file content, supporting HEREDOC.
disableWhen true, disables the entire generate block.

if_exists Options

ValueBehavior
overwriteUnconditionally overwrite the existing file.
overwrite_terragruntTerragrunt overwrites its own generated signature section only.
skipLeave the existing file untouched.
errorThrow an error if the file already exists, halting execution.

Warning

Overwriting files may lead to unintended changes. Review your if_exists setting to prevent data loss.


Example 1: Generate AWS Provider Configuration

This example demonstrates how to generate a providers.tf file for AWS in every module, alongside an S3 remote state configuration.

remote_state {
  backend = "s3"
  config = {
    bucket         = "kodekloud-terragrunt-remote-state"
    key            = "${path_relative_to_include()}/terraform.tfstate"
    region         = "eu-west-1"
    encrypt        = true
    dynamodb_table = "terraform-locks"
  }
}

generate "backend" {
  path      = "backend.tf"
  if_exists = "overwrite_terragrunt"
}

generate "provider" {
  path      = "providers.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "aws" {
  region = "eu-west-1"
}
EOF
}

Example 2: Enforce Provider Version Constraints

To ensure all modules use AWS provider version ~> 5.0, add a versions.tf file:

generate "provider_version" {
  path      = "versions.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}
EOF
}

Combine this with the previous blocks to automate both provider and version file generation.


Best Practices

PracticeBenefit
Centralize common configsEnsures uniformity across modules
Use dynamic content and external filesProvides flexibility and adaptability
Carefully choose if_exists behaviorPrevents accidental overwrites and operational issues

References

Watch Video

Watch video content

Previous
dependencies Block