Skip to content

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

Answered by yorinasub17
yorinasub17 asked this question in Help
Discussion options

You must be logged in to vote

You can do this by using string template directives. For example:

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
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by yorinasub17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
1 participant