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