I'm using Terraform v1.1.0.
I would like to skip both google_folder module resources creation if the GCP Folder structure already exists.
google_folder.department1
google_folder.child_folders
The problem is that I only need to run them once for the first GCP folder structure creation, even though I trigger the project module many times.
terraform/envs/main.tf
variable "folder_name" {
description = "The numeric ID of the folder this project should be created under"
default = "Folder C"
}
module "gcp_project" {
source = "../modules/project"
project_id = var.project_id
folder_name = var.folder_name
}
terraform/modules/project/main.tf
variable "folder_list" {
description = "Folders list"
type = list(string)
default = ["Folder A", "Folder B", "Folder C", "Folder D"]
}
# Top-level folder under an organization.
resource "google_folder" "department1" {
display_name = "Department 1"
parent = "organizations/1234567"
}
# Sub-level folders under the Department 1 folder.
resource "google_folder" "child_folders" {
count = length(var.folder_list)
display_name = element(var.folder_list, count.index)
parent = google_folder.department1.name
}
resource "google_project" "my_project" {
name = "My Project"
project_id = "your-project-id"
folder_id = var.folder_name
}
Also, I tried to use the data.google_active_folder but I got the following error in the first creation of the top-level parent folder under the organization (Department 1)
ERROR Folder not found
How can I solve this issue?