admin管理员组

文章数量:1400028

I am creating a hierarchical folder structure in a GCP bucket using Terraform. Below is the code for your reference

variable.tf

variable "folders" {
  type    = list(string)
  default = ["folder1", "folder2", "folder3", "folder1/subfolder1"]
}

variable "create_storage_folder" {
  description = "Option to create folders"
  type        = bool
  default     = true
}

main.tf

resource "google_storage_bucket" "bucket" {
  name                        = "bucket-123"
  location                    = "europe-west2"
  uniform_bucket_level_access = true
  force_destroy               = true
  storage_class               = "COLDLINE"
  hierarchical_namespace {
    enabled = true
  }
}

resource "google_storage_folder" "folder" {
  count  = var.create_storage_folder ? length(var.folders) : 0
  bucket = google_storage_bucket.bucket.name
  name   = "${var.folders[count.index]}/"
}

When executing the code, I encounter the following error

Error: Error creating Folder: googleapi: Error 409: The parent folder does not exist., conflict
│
│   with google_storage_folder.folder[3],
│   on storage.tf line 12, in resource "google_storage_folder" "folder":
│   12: resource "google_storage_folder" "folder" {

Even though I created folder1 first and then folder1/subfolder1, I'm still encountering this error.

How can I create both the folder and its subfolders using a single resource in GCP bucket using terraform?

I am creating a hierarchical folder structure in a GCP bucket using Terraform. Below is the code for your reference

variable.tf

variable "folders" {
  type    = list(string)
  default = ["folder1", "folder2", "folder3", "folder1/subfolder1"]
}

variable "create_storage_folder" {
  description = "Option to create folders"
  type        = bool
  default     = true
}

main.tf

resource "google_storage_bucket" "bucket" {
  name                        = "bucket-123"
  location                    = "europe-west2"
  uniform_bucket_level_access = true
  force_destroy               = true
  storage_class               = "COLDLINE"
  hierarchical_namespace {
    enabled = true
  }
}

resource "google_storage_folder" "folder" {
  count  = var.create_storage_folder ? length(var.folders) : 0
  bucket = google_storage_bucket.bucket.name
  name   = "${var.folders[count.index]}/"
}

When executing the code, I encounter the following error

Error: Error creating Folder: googleapi: Error 409: The parent folder does not exist., conflict
│
│   with google_storage_folder.folder[3],
│   on storage.tf line 12, in resource "google_storage_folder" "folder":
│   12: resource "google_storage_folder" "folder" {

Even though I created folder1 first and then folder1/subfolder1, I'm still encountering this error.

How can I create both the folder and its subfolders using a single resource in GCP bucket using terraform?

Share Improve this question asked Mar 24 at 22:23 iamarunkiamarunk 1494 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

“Error 409: The parent folder does not exist” occurs because Google cloud storage does not inherently support hierarchical folder structure, instead it uses slashes ( / ) as separators.

As mentioned in the document:

When creating folders, consider the following:

Object and folder names: Buckets with hierarchical namespace enabled, support all valid object names, including those with leading, trailing slashes (/) or consecutive slashes. Each forward slash (/) in an object name represents a folder.

Maximum folder depth: Buckets with hierarchical namespace enabled support a maximum folder depth of 50. As a result, object names cannot have more than 50 slashes (/).

Maximum folder name size: 512 bytes (UTF-8 encoded).

Automatic folder creation: Creating a new object automatically creates any non-existent folders specified in the object's path. For example, creating an object named dir1/foo.txt automatically creates the folder dir1/ if it doesn't already exist.

There are certain limitations while using a hierarchical namespace:

  1. Once the bucket is created hierarchical namespace setting can't be changed

  2. A bucket must also enable uniform bucket-level access. To enable hierarchical namespace.

  3. Autoclass, Bucket Lock, Object holds, Object retention lock,Object versioning are not supported for buckets that use hierarchical namespace.

Refer to the link on google storage folder for a sample piece of script.

You can use the google_storage_bucket_object resource to create folders by defining objects with names ending in / and use for_each to dynamically create multiple folders and sub folders.

Refer to this SO link for more information on google_storage_bucket_object and its example.

本文标签: