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" does not work with map variables #264

Closed
DonEstefan opened this issue Aug 22, 2017 · 3 comments
Closed

"provider" does not work with map variables #264

DonEstefan opened this issue Aug 22, 2017 · 3 comments
Labels

Comments

@DonEstefan
Copy link

Terraform Version

terraform v0.10.2
azurerm_v0.1.5

Affected Resource(s)

provider statement inside of a resource

Terraform Configuration Files

### I try to use "count" and map variables to create multiple resource groups using different provider aliases

provider "azurerm" {
	alias = "international"
...
}
provider "azurerm" {
	alias = "germany"
...
}

#### define a map variable referencing the two providers above
variable "providername" {
  type = "map"
  default = {
    "0" = "azrerm.international"
    "1" = "azrerm.germany"
  }
}

variable "location" {
  type = "map"
  default = {
    "0" = "westeurope"
    "1" = "germanycentral"
  }
}

#create 2 resource groups - each one using a different provider
resource "azurerm_resource_group" "resources" {
  	count = "2"
	provider = "${lookup(var.providername, count.index)}"
	location = "${lookup(var.location, count.index)}"
...
}

Expected Behavior

2 resource groups should be created. One using provider "azrerm.international" and another one using provider "azrerm.germany"

Actual Behavior

I get an error message

* provider.${lookup(var: no suitable version installed
  version requirements: "(any version)"
  versions installed: none

I seems like map variable interpolation does not work in the provider statement
The location statement uses the same mechanism and in this case map variable interpolation works fine.

@tombuildsstuff
Copy link
Contributor

Hey @DonEstefan

Thanks for opening this issue :)

Whilst variable interpolation currently appears to not be possible in the provider block, you can achieve the same thing by wrapping the Azure Region within a module, and then providing the parameters to that module - for instance:

file: ./region/main.tf

variable "location" {}

variable "subscription_id" {}

variable "client_id" {}

variable "client_secret" {}

variable "tenant_id" {}

variable "environment" {
  default = "public"
}

provider "azurerm" {
  subscription_id = "${var.subscription_id}"
  client_id       = "${var.client_id}"
  client_secret   = "${var.client_secret}"
  tenant_id       = "${var.tenant_id}"
  environment     = "${var.environment}"
}

resource "azurerm_resource_group" "resources" {
  name     = "tharvey-${var.location}"
  location = "${var.location}"
}

file: ./main.tf:

module "international" {
  source          = "./region"
  location        = "westeurope"
  subscription_id = "..."
  client_id       = "..."
  client_secret   = "..."
  tenant_id       = "..."
}

module "germanycentral" {
  source          = "./region"
  location        = "germanycentral"
  subscription_id = "..."
  client_id       = "..."
  client_secret   = "..."
  tenant_id       = "..."
  environment     = "german"
}

which when running terraform init && terraform plan returns:

$ terraform plan
...

  + module.germanycentral.azurerm_resource_group.resources
      location: "germanycentral"
      name:     "tharvey-germanycentral"
      tags.%:   "<computed>"

  + module.international.azurerm_resource_group.resources
      location: "westeurope"
      name:     "tharvey-westeurope"
      tags.%:   "<computed>"


Plan: 2 to add, 0 to change, 0 to destroy.

Would it be possible to take a look and see if that solves your issue here? :)

With regards to variable interpolation in the provider block, that's handled in Terraform's Core - once this issue is solved I'll migrate this across to the main repository

Thanks!

@DonEstefan
Copy link
Author

Thanks @tombuildsstuff

Would it be possible to take a look and see if that solves your issue here? :)

Thanks for pointing out this workaround! In one way it does solve the issue. I'd have to rewrite my plans
... from dynamically assigning variables by using maps and count
... to dynamically assigning variables by using modules
I think mixing both can not really be recommended as it increases complexity quite a lot...

With regards to variable interpolation in the provider block, that's handled in Terraform's Core

You are right. The problem ist mentioned several times in the main repository.
Most importantly there is a feature request for this in hashicorp/terraform#3656 "I would like to be able to use a dynamic name for the provider alias inside of a resource definition"

I'll close this case, since it is a tracked in the issue mentioned above.

@ghost
Copy link

ghost commented Apr 1, 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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants