I am attempting to setup a terraform project, using remote storage (Terraform Cloud), that will primarily provision Google Cloud Platform resources. As part of the infrastructure I require 3 environments that will be managed using terraform workspaces. Each environment will have it's own directory in my repository, within each I'll define the environment specific resources. The directory structure looks similar to:
|- terraform-project
|- environments
| |- staging
| | |- main.tf
| | |- outputs.tf
| | |- variables.tf
| |- production
| |- main.tf
| |- outputs.tf
| |- variables.tf
|- backend.tf
|- main.tf
|- outputs.tf
|- variables.tf
However each environment needs to use the same Google Cloud Platform project. I would typically create the project using the following inside the root-most main.tf
file:
resource "random_id" "project_id" {
byte_length = 4
prefix = "${var.project_name}-"
}
resource "google_project" "project" {
name = var.project_name
project_id = random_id.project_id.hex
billing_account = var.billing_account
org_id = var.org_id
}
So my question is how would I create the project only the once and share this between environments? Doing the following inside each environment main.tf
does not work:
resource "google_compute_network" "vpc_network" {
name = "staging-network"
project = google_project.project.project_id
}
The google_project.project.project_id
resource cannot be found. Presumably because the terraform plan environments/{staging,production}
command does not know to look up the directory tree.
I thought about using a module but given the code above uses a random id would this not cause the project to be created once for each environment, but with a different id?
Edit: Another idea is to create a core
workspace that will contain the setup of the Google Cloud Platform project, and any other shared resources. Then each environment will include a data
block pointing to the remote state of the core
workspace:
data "terraform_remote_state" "core" {
backend = "remote"
...
}
resource "google_compute_network" "vpc_network" {
name = "staging-network"
project = data.terraform_remote_state.core.outputs.project_id
}
Is this an acceptable solution?