Skip to content

variables

unit

Usage

With kind = "tfvar", Terragrunt writes the inputs to a terragrunt.auto.tfvars file, which OpenTofu/Terraform loads on its own:

live/vpc/terragrunt.hcl
terraform {
source = "../../modules//vpc"
}
variables {
kind = "tfvar"
inputs = {
cidr_block = "10.0.0.0/16"
}
}
  • Directorylive
    • Directoryvpc
      • terragrunt.hcl
      • Directory.terragrunt-cache/
        • DirectoryzyViUa3fZNBdSZFkV1T2plXXMaM
          • DirectoryCVBDdii3ZDizkp-RK1BYQcZ2j4Y
            • Directoryvpc
              • main.tf
              • terragrunt.auto.tfvars sets cidr_block = "10.0.0.0/16"
  • Directorymodules
    • Directoryvpc
      • main.tf

A configuration can have any number of variables blocks, alongside an inputs attribute. Terragrunt returns an error when one file sets an input twice, in two variables blocks or in a variables block and the inputs attribute.

If a unit defines an input that is also defined by an include, the input in the unit overrides the value from the include. This applies whether each side uses a variables block or the inputs attribute, and the input uses the kind and validate set in the unit. With merge_strategy = "deep", nested maps and lists combine with the include’s value, as they do for the inputs attribute.

Inputs set in a stack’s autoinclude block override the unit’s, and use the kind and validate set in the autoinclude block. An inputs attribute there passes TF_VAR_ environment variables even when the unit uses tfvar or json. To write those inputs to a variables file, set them in a variables block inside autoinclude.

A TF_VAR_ environment variable set before Terragrunt runs, or through env_vars in extra_arguments, takes precedence over an env input. A tfvar or json input takes precedence over it.

In .terragrunt-cache, the variables files stay after the run, so running tofu/terraform directly in that directory loads the tfvar and json inputs. It does not see env inputs, which exist as environment variables set for the invocation of OpenTofu/Terraform. A unit that runs in its own directory with the no-dot-terragrunt-cache experiment has no variables files after the run.

Attributes

inputsmap(any)

A map of variable names to values, written the same way as the inputs attribute.

terragrunt.hcl
variables {
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
subnets = local.subnets
}
}
kindoptional(string, ""env"")

How Terragrunt passes the inputs. env sets TF_VAR_ environment variables, as the inputs attribute does. tfvar writes a terragrunt.auto.tfvars file, and json writes a terragrunt.auto.tfvars.json file, in the directory where OpenTofu/Terraform runs.

terragrunt.hcl
variables {
kind = "tfvar"
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
}
}
nullableoptional(bool, false)

When true, a null value sets its variable to null. Otherwise Terragrunt drops null values, so the variable keeps its default.

terragrunt.hcl
variables {
nullable = true
inputs = {
instance_type = null
}
}
validateoptional(bool, false)

When true, the run stops before OpenTofu/Terraform starts if the module doesn’t declare every variable the block sets.

terragrunt.hcl
variables {
kind = "tfvar"
validate = true
inputs = {
vpc_id = dependency.vpc.outputs.vpc_id
}
}