I work with 2 projects i.e. 2 state files, where I need to access some info from the first one in the second one. So in the first one I declare output.tf
output "project-member_id" {
description = "Project Member ID"
value = rancher2_role_template.project-member.id
}
output "read-only_id" {
description = "Read Only ID"
value = rancher2_role_template.read-only.id
}
output "cluster-read-only_id" {
description = "Cluster Read Only ID"
value = rancher2_role_template.cluster-read-only.id
}
In the second project, I consume it as follows:
locals {
prtbs = flatten([
for projectname,project in local.projects : [
for role, details in project.members: {
....
role_template_id = data.terraform_remote_state.prd-local-state.outputs["${details.role}"]
....
}
]
])
}
If I to a terraform plan
I get this error
│ Error: Unsupported attribute
│
│ on /atlantis-data/repos/Kubernetes/rancher-tf-prd/25/default/prd-project1/projects_namespaces.tf line 7, in locals:
│ 7: role_template_id = data.terraform_remote_state.prd-local-state.outputs["${details.role}"]
│ ├────────────────
│ │ data.terraform_remote_state.prd-local-state.outputs is object with 1 attribute "read-only_id"
│
│ This object does not have an attribute named "cluster-read-only_id".
When I do declare output.tf
in the project 2, where I am referencing the output values from project 1, that works fine
output "project-member_id" {
description = "Project Member ID"
value = data.terraform_remote_state.prd-local-state.outputs["project-member_id"]
}
output "read-only_id" {
description = "Read Only ID"
value = data.terraform_remote_state.prd-local-state.outputs["read-only_id"]
}
output "cluster-read-only_id" {
description = "Cluster Read Only ID"
value = data.terraform_remote_state.prd-local-state.outputs["cluster-read-only_id"]
}
terraform plan
of project 2 shows
Changes to Outputs:
+ cluster-read-only_id = "rt-xxxxx"
+ project-member_id = "rt-xxxxx"
+ read-only_id = "rt-xxxxx"
I have successfully used the same approach in other projects, but somehow here it does not work. If I extend the output.tf
from project 1 as follows
output "project-member_id" {
description = "Project Member ID"
value = rancher2_role_template.project-member.id
}
output "read-only_id" {
description = "Read Only ID"
value = rancher2_role_template.read-only.id
}
output "cluster-read-only_id" {
description = "Cluster Read Only ID"
value = rancher2_role_template.cluster-read-only.id
}
output "roleids" {
description = "IDs for roles"
value = {
read-only_id=rancher2_role_template.read-only.id
project-member_id=rancher2_role_template.project-member.id
cluster-read-only_id=rancher2_role_template.cluster-read-only.id
}
}
And use it as follows in the second project
locals {
prtbs = flatten([
for projectname,project in local.projects : [
for role, details in project.members: {
…
role_template_id = data.terraform_remote_state.prd-local-state.outputs.roleids[“${details.role}”]
…
}
]
])
}
I still get the error
│ Error: Unsupported attribute
│
│ on /atlantis-data/repos/Kubernetes/rancher-tf-prd/25/default/prd-project1/projects_namespaces.tf line 7, in locals:
│ 7: role_template_id = data.terraform_remote_state.prd-local-state.outputs.roleids["${details.role}"]
│ ├────────────────
│ │ data.terraform_remote_state.prd-local-state.outputs is object with 1 attribute "read-only_id"
│
│ This object does not have an attribute named "roleids".
What am I doing wrong?