Consider the following directory structure
- project_resources
- dns
- main.tf
- compute
- main.tf
- dns
- 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.
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"
}
}