Skip to main content
Knowledge Base

Using .tfvars file in terragrunt

Answer

Is it possible to use a value in a `.tfvars` file that I have as part of a `generate` block? ``` generate "k8s" { path = "k8s_provider.tf" if_exists = "overwrite_terragrunt" contents = templatefile( find_in_parent_folders("provider_k8s_for_eks.template.hcl"), { eks_cluster_name = "${local.cluster_name}" }, ) } ``` but I have `cluster_name` in a `tfvars` file that I run as part of part of an `required_var_files`. --- <ins datetime="2022-04-28T14:28:32Z"> <p><a href="https://gruntwork.zendesk.com/agent/tickets/108521">Tracked in ticket #108521</a></p> </ins>

There are two ways to do this: - If these variables are exposed in the module, then you can reference them directly in the generated terraform code as `var`. - One gotcha is that you will need to escape interpolations (e.g., use double dollar like `$${}`) to avoid terragrunt attempting to render them. - Specifically, you can change the `provider_k8s_for_eks.template.hcl` to replace instances of `${eks_cluster_name}` with `$${var.cluster_name}`. - The second approach is to attempt to read the tfvars file into terragrunt to reference them. Unfortunately, we don't have a helper that lets you read in `.tfvars` files in HCL format, so the only way to do this would be to use `json` format and read it in using `jsondecode`. Related: https://github.com/gruntwork-io/terragrunt/issues/1621