Names the block. Names must be unique across a unit and the files it includes. With merge_strategy = "deep" on the include, a unit’s block replaces an included block with the same name.
generate
Usage
Before it runs OpenTofu/Terraform, Terragrunt writes the file into the unit’s copy of the module in .terragrunt-cache:
terraform { source = "../../modules//vpc"}
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOFprovider "aws" { region = "us-east-1"}EOF}Directorylive
Directoryvpc
- terragrunt.hcl
Directory.terragrunt-cache/
DirectoryKscGyaXartMp6_NdWM9ZI_yqw70
DirectorymzEiLIbIiJRsotGoGtH-j-KOhUo
Directoryvpc
- main.tf
- provider.tf written by the
generateblock
Directorymodules
Directoryvpc
- main.tf
A generate block in a file that units include, e.g. root.hcl, gives every unit the same provider or backend configuration.
Setting generate as an attribute
generate also works as an attribute, a map from block names to the same settings. This lets a unit take its generate configuration from another file:
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOFprovider "aws" { region = "us-east-1"}EOF}locals { common = read_terragrunt_config(find_in_parent_folders("common.hcl"))}
generate = local.common.generateLabels
Attributes
The file to write. A relative path resolves against the directory where Terragrunt runs OpenTofu/Terraform.
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOFprovider "aws" { region = "us-east-1"}EOF}What to do when a file exists at path. overwrite replaces it, overwrite_terragrunt replaces it only if Terragrunt generated it and fails otherwise, skip leaves it as it is, and error fails.
generate "provider" { path = "provider.tf" if_exists = "skip" contents = file("provider.tf.tpl")}The contents of the file.
generate "versions" { path = "versions.tf" if_exists = "overwrite_terragrunt" contents = <<EOFterraform { required_version = ">= 1.8"}EOF}Turns off the block, so Terragrunt doesn’t write the file. if_disabled sets what happens to a file already at path.
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" disable = get_env("SKIP_PROVIDER", "false") == "true" contents = file("provider.tf.tpl")}What to do with a file at path when disable is true. remove deletes it, remove_terragrunt deletes it only if Terragrunt generated it and fails otherwise, and skip leaves it.
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" disable = true if_disabled = "remove_terragrunt" contents = ""}The comment syntax for the signature line Terragrunt writes at the top of the file, which overwrite_terragrunt and remove_terragrunt look for. Set it for a file type that doesn’t use # comments.
generate "config" { path = "config.js" if_exists = "overwrite_terragrunt" comment_prefix = "// " contents = "module.exports = {};"}Leaves out the signature line, e.g. for a file format with no comment syntax. Without the signature, overwrite_terragrunt and remove_terragrunt can’t tell that Terragrunt generated the file.
generate "settings" { path = "settings.json" if_exists = "overwrite" disable_signature = true contents = jsonencode({ region = "us-east-1" })}Whether to format the file as HCL. When unset, Terragrunt formats files ending in .tf, .tofu, or .hcl. true formats the file whatever its name, and false writes the contents unchanged.
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" hcl_fmt = false contents = file("provider.tf.tpl")}Writes an ordinary writable file. Otherwise Terragrunt stores the contents in the content-addressable store (CAS), and the file at path is a read-only link to the stored copy, shared by every unit that generates the same contents. Set it when something edits the file in place, e.g. a hook. With --no-cas, Terragrunt writes ordinary files and mutable has no effect.
generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" mutable = true contents = file("provider.tf.tpl")}