TheDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
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
.tfor other configuration files
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. |
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 aproviders.tf file for AWS in every module, alongside an S3 remote state configuration.
Example 2: Enforce Provider Version Constraints
To ensure all modules use AWS provider version~> 5.0, add a versions.tf file:
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 |