'Using multiple providers with one resource in Terraform

I'm new to Terraform and I have an issue I can't seem to find a solution on.

I am using the Oneview provider to connect to two Oneview instances. On each one, I am configuring an NTP server (which is the Oneview IP; this is for testing). My (currently functional) provider code looks like this:

terraform {
  required_providers {
    oneview = {
      source  = "HewlettPackard/oneview"
      version = "6.5.0-13"
    }
  }
}

provider "oneview" {      #These can be replaced with the variables in the variables.tf file
  ov_username   = "administrator"
  ov_password   = "mypassword"
  ov_endpoint   = "https://10.50.0.10/"
  ov_sslverify  = false
  ov_apiversion = 2400
  ov_domain     = "local"
  ov_ifmatch    = "*"
}

provider "oneview" {
  ov_username   = "administrator"
  ov_password   = "mypassword"
  ov_endpoint   = "https://10.50.0.50/"
  ov_sslverify  = false
  ov_apiversion = 3200
  ov_domain     = "local"
  ov_ifmatch    = "*"
  alias = "houston2"
}

and I have the resources in another file:

data "oneview_appliance_time_and_locale" "timelocale" {
}
output "locale_value" {
  value = data.oneview_appliance_time_and_locale.timelocale.locale
}
resource "oneview_appliance_time_and_locale" "timelocale" {
  locale      = "en_US.UTF-8"
  timezone    = "UTC"
  ntp_servers = ["10.50.0.10"]
}



data "oneview_appliance_time_and_locale" "timelocale2" {
}
output "locale_value2" {
  value = data.oneview_appliance_time_and_locale.timelocale.locale
}
resource "oneview_appliance_time_and_locale" "timelocale2" {
  locale      = "en_US.UTF-8"
  timezone    = "UTC"
  ntp_servers = ["10.50.0.50"]
  provider = oneview.houston2
}

What I'd like to do is set it up in a way that I can do some sort of "for each provider, run the resource with the correct ntp_server variable", instead of writing a resource for every provider. So for each loop of the resource, it would use the right provider and also grab the right variable for the ntp server.

From what I've read, Terraform doesn't really use traditional for_each statements in a way that I'm used to, and I'm kind of stumped as to how to accomplish this. Does anyone have any suggestions?

Thank you very much for all your help!



Solution 1:[1]

resource "oneview_appliance_time_and_locale" "timelocale2" {
  for_each = var.provider_list // List contain provider and its alias
  locale      = "en_US.UTF-8"
  timezone    = "UTC"
  ntp_servers = ["10.50.0.50"]
  provider = each.alias
}

Can we try this way, loop through the provider list.. Terraform is supporting the same.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jijo Alexander