Trying to access output from remote state

Hey all,
Want to be able to get remote state output ( instance ids )and use that in a custom module that creates an ALB .
Existing code works - I can add multiple members with multiple data blocks defined

data “terraform_remote_state” “mydata” {
backend = “s3”
config = {
region = var.region
bucket = “abc-{var.region}-{xxx}-tf-states”
key = “env:/${var.environment}-abc/terraform.tfstate”
}
}

module “mymodule” {

members = [
data.terraform_remote_state.mydata.outputs.instance-id

]

}

Refactored code throws an error on plan - This object does not have an attribute named instance-id .

locals {
members = {
default = [“env:/{var.environment}-xxx/terraform.tfstate", "env:/{var.environment}-xxy/terraform.tfstate”,]
dev = [“env:/{var.environment}-xxx/terraform.tfstate", "env:/{var.env2}-xxc/terraform.tfstate”]

}
}

data “terraform_remote_state” “mydata” {
for_each = lookup(local.members, var.env2, “default”)
backend = “s3”
config = {
region = var.region
bucket = “abc-{var.region}-{xxx}-tf-states”
key = each.value
}
}

module “mymodule” {

members = [for outputs in data.terraform_remote_state.mydata : outputs.instance-id ]

}

I messed around with it and got it to work - just posting here if it helps. Since the custom module accepts a list for members - I used this

members = tolist([for outputs,state in data.terraform_remote_state.mydata : state.outputs.instance-id])

1 Like