diff --git a/README.md b/README.md index 406a253c..03d41136 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ Functional examples are included in the | names | Bucket name suffixes. | `list(string)` | n/a | yes | | prefix | Prefix used to generate the bucket name. | `string` | n/a | yes | | project\_id | Bucket project id. | `string` | n/a | yes | +| randomize\_suffix | Adds an identical, but randomized 4-character suffix to all bucket names | `bool` | `false` | no | | retention\_policy | Map of retention policy values. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket#retention_policy | `any` | `{}` | no | | set\_admin\_roles | Grant roles/storage.objectAdmin role to admins and bucket\_admins. | `bool` | `false` | no | | set\_creator\_roles | Grant roles/storage.objectCreator role to creators and bucket\_creators. | `bool` | `false` | no | diff --git a/main.tf b/main.tf index 5cfd7f14..cc646c20 100644 --- a/main.tf +++ b/main.tf @@ -14,8 +14,16 @@ * limitations under the License. */ +/****************************************** + Bucket random id suffix configuration + *****************************************/ +resource "random_id" "bucket_suffix" { + byte_length = 2 +} + locals { prefix = var.prefix == "" ? "" : join("-", [var.prefix, lower(var.location), ""]) + suffix = var.randomize_suffix ? "-${random_id.bucket_suffix.hex}" : "" names_set = toset(var.names) buckets_list = [for name in var.names : google_storage_bucket.buckets[name]] first_bucket = local.buckets_list[0] @@ -32,7 +40,7 @@ locals { resource "google_storage_bucket" "buckets" { for_each = local.names_set - name = "${local.prefix}${lower(each.value)}" + name = "${local.prefix}${lower(each.value)}${local.suffix}" project = var.project_id location = var.location storage_class = var.storage_class diff --git a/variables.tf b/variables.tf index a3f880f0..f7b121da 100644 --- a/variables.tf +++ b/variables.tf @@ -29,6 +29,12 @@ variable "names" { type = list(string) } +variable "randomize_suffix" { + description = "Adds an identical, but randomized 4-character suffix to all bucket names" + type = bool + default = false +} + variable "location" { description = "Bucket location." type = string