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
if_exists Options
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: