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
Attribute | Description |
---|---|
name | Identifier for the generate block, useful when defining multiple blocks. |
path | Relative output path for the generated file in the Terragrunt working directory. |
if_exists | Action if the file already exists. See values in the table below. |
comment_prefix | Prefix for comments in the generated file (default: # ). |
disable_signature | When true , suppresses the Terragrunt signature comment in the generated file. |
contents | The file content, supporting HEREDOC. |
disable | When true , disables the entire generate block. |
if_exists Options
Value | Behavior |
---|---|
overwrite | Unconditionally overwrite the existing file. |
overwrite_terragrunt | Terragrunt overwrites its own generated signature section only. |
skip | Leave the existing file untouched. |
error | Throw 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
Practice | Benefit |
---|---|
Centralize common configs | Ensures uniformity across modules |
Use dynamic content and external files | Provides flexibility and adaptability |
Carefully choose if_exists behavior | Prevents accidental overwrites and operational issues |
References
Watch Video
Watch video content