Terragrunt Note
Terragrunt wraps Terraform to keep configurations DRY across environments. The config file is named terragrunt.hcl.
Core blocksβ
| Block | Purpose |
|---|---|
terraform | Points to the Terraform source module and adds extra CLI arguments or hooks. |
include | Inherits a parent terragrunt.hcl so environment configs reuse shared settings. |
locals | Defines aliases and computed values inside the current file. |
remote_state | Configures the backend (S3, GCS, Azure, etc.) once for all child configs. |
generate | Creates additional Terraform files, such as a backend configuration, at runtime. |
Example: backend generationβ
# terragrunt.hcl
remote_state {
backend = "s3"
config = {
bucket = "terraform-statefiles-aws-vpc"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-west-2"
encrypt = true
dynamodb_table = "terraform-locks"
}
generate = {
path = "s3-backend.tf"
if_exists = "overwrite_terragrunt"
}
}
This generates s3-backend.tf in each child directory with the S3 backend configuration.
Example: include a parent configβ
# dev/terragrunt.hcl
include "root" {
path = find_in_parent_folders("root.hcl")
}
find_in_parent_folders() walks up the directory tree until it finds root.hcl. Each child can include only one block with the same label, but different labels are allowed if you need more than one inclusion.
Example: terraform source and hooksβ
# terragrunt.hcl
terraform {
source = "git::https://github.com/org/terraform-modules.git//vpc?ref=v1.2.0"
extra_arguments "common_vars" {
commands = [
"apply",
"plan",
]
arguments = [
"-var-file=../common.tfvars",
]
}
before_hook "before_hook" {
commands = ["apply"]
execute = ["echo", "Running apply"]
}
}
Completion criterionβ
After reading this, you should be able to:
- Name the config file and place it correctly in a Terragrunt project.
- Use
includeto inherit a parent configuration. - Use
remote_statewithgenerateto create a backend file automatically. - Add extra arguments and hooks to Terraform commands.