Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider configurations not evaluated correctly when in a module using count #24851

Closed
marinsalinas opened this issue May 4, 2020 · 5 comments · Fixed by #24892
Closed

provider configurations not evaluated correctly when in a module using count #24851

marinsalinas opened this issue May 4, 2020 · 5 comments · Fixed by #24892
Assignees
Labels
v0.13 Issues (primarily bugs) reported against v0.13 releases

Comments

@marinsalinas
Copy link

marinsalinas commented May 4, 2020

Terraform Version

Terraform v0.13.0-dev
+ provider registry.terraform.io/hashicorp/aws v2.59.0

Terraform Configuration Files

./modules/test/main.tf
variable "region" {}

provider "aws" {
  version = "~> 2.59.0"
  region  = var.region
  alias   = "this"
}

data "aws_region" "current" {
  provider = aws.this
}
./main.tf
locals {
  secondary_clusters = [
    {
      region        = "us-east-2"
      replicas      = 3
      instance_size = "db4.large"
    },
    {
      region        = "us-east-1"
      replicas      = 5
      instance_size = "db4.large"
    },
    {
      region        = "us-west-2"
      replicas      = 12
      instance_size = "db4.large"
    }
  ]

}

module "test" {
  count  = length(local.secondary_clusters)
  source = "./modules/test"
  region = local.secondary_clusters[count.index].region
}

Debug Output

https://gist.github.com/marinsalinas/8dea55ed182bc9b6991739bf27c5c7a6

Crash Output

Expected Behavior

Initialize provider by region.

Actual Behavior

The region variable is not recognized in the provider block when use count.

Steps to Reproduce

  1. terraform init
  2. terraform plan

Additional Context

We are waiting for this feature on Terraform since we need to create RDS regional clusters dynamically by a list of regions provided.

References

@danieldreier danieldreier self-assigned this May 4, 2020
@danieldreier
Copy link
Contributor

Hi @marinsalinas! I'm thrilled that you're trying out 0.13. Thanks! The module count feature isn't quite done in 0.13, so I'm not surprised you're having problems. I'll talk with the lead engineer on this feature and see what the expected behavior is here.

We are expecting to do a public beta of 0.13.0. Since you're already looking into it, you seem like a great person to get feedback from when that's available.

@jbardin
Copy link
Member

jbardin commented May 4, 2020

Hi @marinsalinas,

Thanks for trying things out!

The underlying issue here is that you cannot have a provider configuration within a module that will be expanded. Due to some dependent work being sorted out, we have not implement the validation for this yet. In the future this config would fail with an error explaining the problem, instead of just silently failing to evaluate the provider.

While the ability to iterate over providers is something that would be used with module expansion, it is a separate feature in and of itself. What you are looking for is being tracked in #24476, which is a narrower use case of #9448.

@marinsalinas
Copy link
Author

marinsalinas commented May 8, 2020

Oh, awesome! So following this idea to not use a provider configuration inside modules that will be expanded, I'm wondering if this configuration should work:

./modules/test/main.tf

data "aws_region" "current" {}
./main.tf
locals {
  secondary_clusters = [
    {
      region        = "us-east-2"
      replicas      = 3
      instance_size = "db4.large"
    },
    {
      region        = "us-east-1"
      replicas      = 5
      instance_size = "db4.large"
    },
    {
      region        = "us-west-2"
      replicas      = 12
      instance_size = "db4.large"
    }
  ]

}



provider "aws" {
  count = length(local.secondary_regions)
  region = local.secondary_regions[count.index].region
  alias   = local.secondary_regions[count.index].region
}

module "test" {
  count  = length(local.secondary_clusters)
  providers = {
    aws = "aws.${local.secondary_clusters[count.index].region}" //maybe - aws = "aws[count.index]"
  }
  source = "./modules/test"
  region = local.secondary_clusters[count.index].region
}

cc @jbardin @pselle

@pselle
Copy link
Contributor

pselle commented May 8, 2020

@marinsalinas Not quite -- your example includes a few things that won't work. I recommend checking out the related issues @jbardin linked as they're relevant to what you're trying to do here :)

Meanwhile, we interpreted the issue you opened here as "provider blocks in modules using count aren't working" and I've merged a PR that gives an error message clarifying that you cannot configure providers in a module that will expand (count or for_each).

Thank you for the report!

@apparentlymart apparentlymart added v0.13 Issues (primarily bugs) reported against v0.13 releases and removed v0.13-dev labels Jun 2, 2020
@apparentlymart apparentlymart changed the title Terraform 0.13 Dev - variables are not recognized in provider block when use count. provider configurations not evaluated correctly when in a module using count Jun 2, 2020
@ghost
Copy link

ghost commented Jun 8, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jun 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
v0.13 Issues (primarily bugs) reported against v0.13 releases
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants