Terragrunt: Using Terragrunt for modules with multiple providers

Created on 18 Mar 2020  路  3Comments  路  Source: gruntwork-io/terragrunt

How does Terragrunt call into modules with multiple providers? I'm going with the approach of keeping the code DRY and keeping only terragrunt.hcl files in my directories, which is partitioned by environment and component.

For example, AWS VPC peering is a common component that requires 2 providers as there's both a requester and a provider. In regular Terraform, I'd do something like

module "vpc_peering" {
  source = "../path/vpc-peer"
  ... (arguments)

  providers = {
    aws.requester = aws.default
    aws.accepter = aws.peer
  }
}

However, if I'm using a root level terragrunt.hcl and then a directory level terragrunt.hcl that includes the root level one and has terraform and input blocks, I'd have something like:

# root level terragrunt.hcl
remote_state {
  backend "s3"
  (some config)
}

generate "provider" {
  (provider config for both aws.default and aws.peer)
}

# directory level terragrunt.hcl
terraform {
  source = "../path/vpc-peer"
}

include {
  path = find_in_parent_folders()
}

inputs = {
  (arguments)
}

How would one pass multiple providers to the module?

enhancement help wanted

Most helpful comment

Unfortunately, this is not something terragrunt can handle in the current design (because terraform doesn't provide us the options to update providers on the CLI), so even if we open a ticket, we would most likely close it out as something we won't implement, or will be ignored for years. The only way I see terragrunt supporting this is if we implement https://github.com/gruntwork-io/terragrunt/issues/759, but that won't happen for a while.

The workarounds I mentioned above, however painful it is, is the only options you have right now if you want to customize a provider defined in a module with terragrunt.

All 3 comments

Terragrunt currently doesn't support passing in multiple provider aliases to a module directly. I'm not sure terraform supports setting that on the CLI either so this might not be possible to implement.

That said, there are two possible workarounds for this:

  • Create a wrapper terraform module that manually configures the providers and sets the setting on the module.

  • In theory, you should be able to override the providers for the module directly using the generate block. So instead of calling the, default and peer, you call the providers requester and accepter, which would then be injected directly into the module.

@yorinasub17 - I know this got closed, but I would love to see this get added as an enhancement. Wasn't sure if I should open a "duplicate" issue or try to get this re-opened.

As to the first option - I'd have to map all the input vars and output vars and do it for every module that is looking for multiple providers. It would also mean not being able to leverage all the provider blocks generated previously, which would add to duplicated code and potentially confusion since resource creation would be decoupled from those provider blocks. Unless I'm just missing a way easier implementation here.

The second of the approaches you mentioned just errors with:
~
Error: Duplicate provider configuration
~

You could remove the two provider blocks in the vpc-peering module, but then everytime you this happens in a community module you're forking and maintaining your own code base and/or you're changing your providers to match what each module might call them, default peer accepter etc..

Apologies on any ignorance towards terraform/terragrunt here. Been thrown in the deep end here.

Unfortunately, this is not something terragrunt can handle in the current design (because terraform doesn't provide us the options to update providers on the CLI), so even if we open a ticket, we would most likely close it out as something we won't implement, or will be ignored for years. The only way I see terragrunt supporting this is if we implement https://github.com/gruntwork-io/terragrunt/issues/759, but that won't happen for a while.

The workarounds I mentioned above, however painful it is, is the only options you have right now if you want to customize a provider defined in a module with terragrunt.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mpkerr picture mpkerr  路  3Comments

ozbillwang picture ozbillwang  路  3Comments

dmlemos picture dmlemos  路  3Comments

shaharmor picture shaharmor  路  3Comments

antonbabenko picture antonbabenko  路  3Comments