Skip to main content
Knowledge Base

[terragrunt] How can you conditionally include a provider in a generate block?

Answer

I'm aiming to consolidate all providers here, however I have one provider that requires authentication against a remote source to work. This provider has very limited use and I wouldn't want to authenticate against every run. However if I could conditionally include this provider based on the use of certain modules that would be ideal.

You can do this by using [string template directives](https://www.terraform.io/language/expressions/strings#directives). For example: ```hcl locals { # Assuming you have a folder structure like ACCOUNT/REGION/ENV, you can have a # region.hcl file like ACCOUNT/REGION/region.hcl to dynamically change what gets loaded here. region_vars = read_terragrunt_config(find_in_parent_folders("region.hcl")) } generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = <<EOF provider "aws" { alias = "us-east-1" region = "us-east-1" } %{ if local.region_vars.locals.region == "us-west-1" } provider "aws" { alias = "us-west-1" region = "us-west-1" } %{ endfor } EOF } ```