The Terraform block in Terragrunt defines how Terragrunt locates and invokes your Terraform configuration. It centralizes settings such as module source, version locking, extra CLI arguments, and lifecycle hooks. By configuring this block once, you ensure consistent Terraform behavior across all environments and modules.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
Key Terraform Block Attributes
| Attribute | Description | Example |
|---|---|---|
source | Location of the Terraform module (Registry, Git repo, or local path) | source = "tfr://terraform-aws-modules/vpc/aws/?version=5.8.1" |
version / ref | Locks a Registry module or Git source to a specific version or commit | ?version=5.8.1 (Registry) / ?ref=v1.2.0 (Git) |
extra_arguments | Defines additional CLI flags or variable files to pass to Terraform | arguments = ["-var-file=prod.tfvars"] |
hooks | before_hook or after_hook blocks for executing scripts around Terraform commands | before_hook "lint" { commands = ["plan"]; execute = ["npm","run","lint"] } |
Always lock your modules to a specific
For Registry modules, append
version or ref to avoid unintended upgrades.For Registry modules, append
?version=, and for Git sources, use ?ref=.1. Defining the Source
Thesource attribute tells Terragrunt where to fetch Terraform code. You can reference:
- Terraform Registry Modules
- Git Repositories (HTTPS or SSH)
- Local Directories
1.1 Terraform Registry
1.2 GitHub (HTTPS)
1.3 GitHub (SSH)
1.4 Local Modules
Ideal for local development and testing:2. Passing Extra Arguments
Useextra_arguments to pass flags, variable files, or arbitrary flags to Terraform:
| Field | Description |
|---|---|
commands | List of Terraform commands (e.g., plan, apply) |
arguments | Array of CLI flags or variable file references |
3. Hooks and Custom Commands
Hooks allow you to run scripts before or after Terraform commands:| Hook Type | Purpose |
|---|---|
before_hook | Run tasks prior to Terraform execution |
after_hook | Run cleanup or notifications post Terraform run |
Use hooks carefully in production: unexpected script failures can block Terraform runs.
Always test hooks in a development environment first.
Always test hooks in a development environment first.