0

My infrastructure its composed by a Host Project and several Service Projects that are using its Shared VPC.

I have refactored my .tf files of my infrustructure as it follows:

├── env
|   ├── dev
│       ├── main.tf
│       ├── outputs.tf
│       └── variables.tf
│   ├── pre
│   └── pro
├── host
│   ├── main.tf
│   ├── outputs.tf
│   ├── terraform.tfvars
│   └── variables.tf
└── modules
    ├── compute
    ├── network
    └── projects

The order of creation of the infrastructure is:

  1. terraform apply in /host
  2. terraform apply in /env/dev (for instance)

In the main.tf of the host directory I have created the VPC and enabled Shared VPC hosting:

# Creation of the hosted network
resource "google_compute_network" "shared_network" {
  name                    = var.network_name
  auto_create_subnetworks = false
  project                 = google_compute_shared_vpc_host_project.host_project.project
  mtu                     = "1460"
 
}

# Enable shared VPC hosting in the host project.
resource "google_compute_shared_vpc_host_project" "host_project" {
  project    = google_project.host_project.project_id
  depends_on = [google_project_service.host_project]
}

The issue comes when I have refer to the Shared VPC Network in the Service Projects.

In the main.tf from env/dev/ I have set the following:

resource "google_compute_shared_vpc_service_project" "service_project_1" {
  host_project    = google_project.host_project.project_id
  service_project = google_project.service_project_1.project_id

  depends_on = [
    google_compute_shared_vpc_host_project.host_project,
    google_project_service.service_project_1,
  ]
}

QUESTION

How do I refer to the Host Project ID from another directory in the Service Project?

What I have tried so far

  1. I have thought of using Ouput Values and Data Sources:

    In the host/outputs.tf declared as an output the Project ID as:

    output "project_id" {
        value = google_project.host_project.project_id
     }
    

    But then I end up not knowing how to implement this output in my env/dev/main.tf

  2. I have thought on Data Sources and, in the env/dev/main.tf fetch for the Host Project ID. But then, in order to fetch it, I would need its name (which breaks the purpose of providing it in a programatic way if I have to hardcode it).

What should I try next? What I am missing?

1 Answer 1

3

The files under the env/dev folder can't see anything above it, only any referenced modules.
You could refactor the host folder into a module to allow access to it's outputs... but that adds a risk that the host will be destroyed whenever you destroy a dev environment.
I would try running terraform output -raw project_id after creating the host and piping it to a text file or environment variable. Then using that as the input for a new "host_project" or similar variable in the 'env/dev' deployment.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.