Terragrunt: Feature request: Add a option to the generate block to enable skipping the generation of the file

Created on 17 Sep 2020  路  3Comments  路  Source: gruntwork-io/terragrunt

I have a use case where I have multiple modules that might use Kubernetes and Helm providers.

To avoid repeating the code for those providers, I have it generated on the parent terragrunt.hcl file like this:

generate "provider-kubernetes" {
  path      = "provider-kubernetes.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "kubernetes" {
  ...
}
EOF
}

generate "provider-helm" {
  path      = "provider-helm.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "helm" {
  kubernetes {
    ...
  }
}
EOF
}

Note: I used ... here to hide the configuration I use for the providers, as it doesn't matter for the example in question.

However, the problem here is that some modules on sub-folders might not need the helm provider, and some do.

What I want is to be able to optionally generate the file depending a boolean flag. Right now I am doing it like this:

generate "provider-helm" {
  path      = "provider-helm.tf"
  if_exists = "overwrite_terragrunt"
  contents  = local.enable_helm_provider ? "" : <<EOF
...
EOF
}

The flag local.enable_helm_provider is a bool that is loaded from the child modules.
This solution works, but I still have a empty provider-helm.tf file generated.

It would be really nice if Terragrunt had a attribute to support skipping the file creation, such as this:

generate "provider-helm" {
  path      = "provider-helm.tf"
  if_exists = "overwrite_terragrunt"
  generate_if = local.enable_helm_provider
  contents  =  <<EOF
...
EOF
}

The file would only be generated when generate_if is true.

Maybe this is not hard to implement and would be beneficial for more developers?
Are there any other options I am not aware of?

Thanks.

enhancement help wanted

All 3 comments

This seems like a useful addition, and a PR to add this would be welcome!

Here is a potential workaround to this as well, assuming you will always generate the kubernetes provider. In that case, you can combine the two into one file and use template directives to only generate the helm portion if enabled.

E.g.:

generate "provider-kubernetes" {
  path      = "provider-kubernetes.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
provider "kubernetes" {
  ...
}

%{ if local.enable_helm_provider }
provider "helm" {
  kubernetes {
    ...
  }
}
%{ endif }
EOF
}

That's a nice approach @yorinasub17 ... I think you could generalize it to handle all providers, so it's not dependent on the kubernetes provider...

generate "providers" {
  path      = "providers.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
%{ if local.enable_kubernetes_provider }
provider "kubernetes" {
  ...
}
%{ endif }

%{ if local.enable_helm_provider }
provider "helm" {
  kubernetes {
    ...
  }
}
%{ endif }
EOF
}

Thanks for the templating suggestion, I will try that for now as it's even better.
But, this approach still generate the files, it would be good to have a option that would avoid generation.

Was this page helpful?
0 / 5 - 0 ratings