Use Terraform state output from another project

8.4k Views Asked by At

I recently upgraded Terraform from 11.11 to 12.24, and after running the 0.12upgrade, accessing the remote terraform_state output no longer works.

The error I get is : This object does not have an attribute named "subnet_id"

This is my config:

Repo 1 (creates networks, subnets, etc)

network 
| main.tf 
| output.tf

Contents of output.tf (from above):

output "subnet_ids" {
  value = openstack_networking_subnet_v2.openstack-subnet.*.id
}

output "network_ids" {
  value = openstack_networking_network_v2.openstack-networks.*.id
}

Running Terraform output in above repo, gives me the following output (modified id's):

network_ids = [
  "08adfe73-dfg5-404d-958e-e8db73121531",
  "c0b561e5-320c-46b3-b328-98723f54ef82",
  "200eb570-b734-4b18-9250-6ckae90ea0e1",
  "84c43fc5-771c-4c79-8670-3d858788661e",
]
subnet_ids = [
  "f5df394f-d542-492a-a224-eefb998536ac",
  "f0f5d2fe-e83c-4041-971a-bba34870d5de",
  "89e966b1-826e-483d-b312-bb7aa9893a02",
  "76d6dfda-8161-4961-89a6-39aeeb82db3c",
]

Repo 2 (Creates compute platform)

infrastructure
| main.tf
  | modules 
    | compute
      | main.tf

Contents of the compute's main.tf:

...
data "terraform_remote_state" "base_networking_a" {
  backend = "s3"
  config = {
    bucket               = "terraform-state"
    workspace_key_prefix = "network-terraform-state/${var.environment}a"
    key                  = "terraform.tfstate"
    region  = "ap-southeast-2"
    profile = "aws_profile"
  }
}
...
resource "openstack_lb_loadbalancer_v2" "k8s-loadbalancer-a" {
  name          = var.loadbalancer_name
  vip_subnet_id = "data.terraform_remote_state.base_networking_a.outputs.subnet_ids[2]"
}

As an additional method, when I output the contents using a terraform file with the following contents :

output "net" {
    value = data.terraform_remote_state.base_networking_a.outputs.*
}

Then I get the following ouput:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

net = [
  {},
]

Any ideas how to access those values stored in a remote state, or what I'm doing wrong?

1

There are 1 best solutions below

0
On

Here is an example that is working fine for me.

Repo 1 output.tf

output "route53_zone_com_example_public_id" {
  description = "The Route53 Zone ID."
  value       = aws_route53_zone.com_example_public.id
}

Repo 2 main.tf

data "terraform_remote_state" "route53_zones" {
  backend = "s3"
  config = {
    bucket = "tf-state-route53"
    key    = "zones/terraform.tfstate"
    region = "eu-west-1"
  }
}

resource "aws_route53_record" "com_example_template_A_1" {
  zone_id = data.terraform_remote_state.route53_zones.outputs.route53_zone_com_example_public_id
  name    = "template-example.example.com"
  type    = "A"
  ttl     = "300"
  records = ["127.0.0.1"]
}