Quantcast
Channel: Terraform - HashiCorp Discuss
Viewing all articles
Browse latest Browse all 11451

Is it possible to use variable data sources in a for_each?

$
0
0

I have a couple of remote state datasources based on AWS account names whose purpose it is to get the account ID.

I something similar to this in a data_sources.tf:

data "terraform_remote_state" "account1" {
    backend = "s3"
    config = {
      bucket = blah
      ...etc
    }
  }
  data "terraform_remote_state" "account2" {
   ...etc
  }

in my main.tf, I’m creating a list variable with the account names, and I’d like to do a for_each on the list of account names and use their associated data sources to retrieve the accountID on-the-fly.
I’ve tried something like below, but can’t get the syntax right and I’m not even sure it’s possible to do this, but I’m hopeful.

variable "accounts" {
  type = list
  default = [
    "account1",
    "account2"
  ]
}

module "test" {
  source = mysource
  for_each = toset(var.accounts)
  account_number = data.terraform_remote_state.${each.key}.outputs.account_id

Any help or suggestions are greatly appreciated. I’ve been working on this and googling for a couple hours now and I’m stuck.

3 posts - 2 participants

Read full topic


Viewing all articles
Browse latest Browse all 11451

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>