Use non Hashicorp provider in module with for_each

I’m using the community Snowflake provider https://registry.terraform.io/providers/chanzuckerberg/snowflake/latest

I’ve created project that has a root terraform file and also a module that uses this provider e.g.

my_project
   |_
      main.tf
      modules
         |_
            my_mod
              |_
                 main.tf
   

In the root main.tf file I’ve defined these providers:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.40.0"
    }
    snowflake = {
      source  = "chanzuckerberg/snowflake"
      version = "~> 0.25.12"
    }
  }

However when I run terraform init with only the above in the root module I get this error:


 Error: Failed to query available provider packages
│
│ Could not retrieve the list of available versions for provider hashicorp/snowflake: provider registry registry.terraform.io does not have a provider named
│ registry.terraform.io/hashicorp/snowflake
│
│ Did you intend to use chanzuckerberg/snowflake? If so, you must specify that source address in each module which requires that provider. To see which modules are      
│ currently depending on hashicorp/snowflake, run the following command:
│     terraform providers

It seems like because it’s not a Hashicorp provider (the source being chanzuckerberg/snowflake rather than hashicorp/snowflake) I have to specify the provider in the module itself rather than in the root and have it be inherited by the module.

So in the module (“my_mod”) main.tf file I have also defined the same provider:

terraform {
  required_providers {
    snowflake = {
      source  = "chanzuckerberg/snowflake"
      version = "~> 0.25.10"
    }
  }
}

This works fine until I try and use a for_each in the calling module block in the root main.tf, e.g.

module "my_mod" {
  source                 = "./modules/my_mod"
  for_each               = local.list_of_buckets
  s3_bucket              = "${each.key}"
}

which generates this error:

│ Error: Module module.my_mod contains provider configuration
│
│ Providers cannot be configured within modules using count, for_each or depends_on.
╵

Since I apparently must define the provider in the module itself because it’s not a Hashicorp one, does this mean there is no way to use a for_each in the calling module block in the root, or is there some other way to configure this so that the module will inherit the correct provider settings from the root?

Hi @nathan-protempo,

It is correct that each module must declare which providers it expects, but the error shown indicates that you have a provider block somewhere in the module which is not allowed.

If your module requires a specific configuration different from the root module, you can configure the provider in the root module, and pass the provider configuration into the module using the providers map within the module block.

See Providers Within Modules - Configuration Language - Terraform by HashiCorp for more details.

Thanks!