Terraform 0.13 module loop limitation?

Hi all :wink:

Seems to have some limitations in the new loop feature for module… but I don’t really understant why :frowning:

I have a map with informations about some accounts

Now… I want to create a module to deploy, for example, a SQS Queue in each account.
Code example in my module :

provider "aws" {
  region  = var.region
  profile = var.profile

  assume_role {
    role_arn     = format("arn:aws:iam::%s:role/%s", var.account_id, var.role_name)
    session_name = "newsession"
  }
}

resource "aws_sqs_queue" "test" {
  name_prefix = "thequeue"
}

And so, in my root configuration, I try to loop over my map and call my module for each account infos :

module "setup" {
  source = "./modules/setup"

  for_each = local.account_map

  profile    = "myprofile"
  region     = "eu-west-1"
  account_id = each.value.id
  role_name  = var.iam_cross_role_name
}

But terraform returns this error :

Module “setup” cannot be used with count because it contains a nested provider
configuration for “aws”, at modules\setup\provider.tf:1,10-15.

This module can be made compatible with count by changing it to receive all of
its provider configurations from the calling module, by using the “providers”
argument in the calling module block.

I really don’t understand why defining a provider in a module cause a problem for the loop feature ?

A reason ? a solution ?

Thanks you very much :wink:

1 Like

I have the same problem :frowning_face:

The PR adding this limitation (https://github.com/hashicorp/terraform/pull/24892) seems to suggest it was a technical issue, so I’m guessing this isn’t on the roadmap to be fixed soon, but I would be very interested to hear from a maintainer with context as to whether this is something that may be supported in future? Sounds like it’s related to https://github.com/hashicorp/terraform/issues/24476 based on jbardin’s comment here: https://github.com/hashicorp/terraform/issues/24851