Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reference outputs from another local project in terraform

Consider the following directory structure

  • project_resources
    • dns
      • main.tf
    • compute
      • main.tf
  • development
    • main.tf
  • production
    • main.tf
  • global
    • main.tf

In my TF, I have a global module, which should only be run once. i.e. set up DNS with ACM etc.

Once the global module is setup, I may apply development or production. However, for both production and development, i need to set a record in my DNS – hence I need to be able to access the id of the DNS zone from the global module.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For the global env I obviously export the output from my DNS zone, e.g.:

output "route53_zone_id" {
    value = aws_route53_zone.proj_hosted_zone.id
}

Since I’m treating global, production and dev as separate "projects", I cannot reference route53_zone_id from a module import, so I did something along the lines of this (inspired by another SO post):

data "global_state" "global" {

  backend = "local"
  config = {
    path = "../global/terraform.tfstate"
  }
}

locals {
    dns_servers = data.global_state.global.outputs.route53_zone_id
}

but once I run terraform init, it seems as if terraform thinks that the global data object is a provider, since I get the following error:

Error: Failed to query available provider packages. Could not retrieve the list of available versions for provider hashicorp/global: provider registry registry.terraform.io does not have a provider named registry.terraform.io/hashicorp/global

I’m therefore keen to understand, what I am missing in terms of getting the output variables from the global project in respectively development and production environments.

>Solution :

This should work but with the terraform_remote_state data source:

data "terraform_remote_state" "global" {
  backend = "local"

  config = {
    path = "../global/terraform.tfstate"
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading