diff --git a/README.md b/README.md index 0e14608..27ad250 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ module "localhost_function" { | bucket\_force\_destroy | When deleting the GCS bucket containing the cloud function, delete all objects in the bucket first. | bool | `"false"` | no | | bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map(string) | `` | no | | bucket\_name | The name to apply to the bucket. Will default to a string of the function name. | string | `""` | no | +| create\_bucket | Whether to create a new bucket or use an existing one. If false, `bucket_name` should reference the name of the alternate bucket to use. | bool | `"true"` | no | | description | The description of the function. | string | `"Processes events."` | no | | entry\_point | The name of a method in the function source which will be invoked when the function is executed. | string | n/a | yes | | environment\_variables | A set of key/value environment variable pairs to assign to the function. | map(string) | `` | no | diff --git a/main.tf b/main.tf index 5795c95..39c78de 100644 --- a/main.tf +++ b/main.tf @@ -48,6 +48,7 @@ data "archive_file" "main" { } resource "google_storage_bucket" "main" { + count = var.create_bucket ? 1 : 0 name = coalesce(var.bucket_name, var.name) force_destroy = var.bucket_force_destroy location = var.region @@ -59,7 +60,7 @@ resource "google_storage_bucket" "main" { resource "google_storage_bucket_object" "main" { name = "${data.archive_file.main.output_md5}-${basename(data.archive_file.main.output_path)}" - bucket = google_storage_bucket.main.name + bucket = var.create_bucket ? google_storage_bucket.main[0].name : var.bucket_name source = data.archive_file.main.output_path content_disposition = "attachment" content_encoding = "gzip" @@ -85,7 +86,7 @@ resource "google_cloudfunctions_function" "main" { labels = var.labels runtime = var.runtime environment_variables = var.environment_variables - source_archive_bucket = google_storage_bucket.main.name + source_archive_bucket = var.create_bucket ? google_storage_bucket.main[0].name : var.bucket_name source_archive_object = google_storage_bucket_object.main.name project = var.project_id region = var.region diff --git a/variables.tf b/variables.tf index a3a9554..68d4462 100644 --- a/variables.tf +++ b/variables.tf @@ -106,6 +106,12 @@ variable "bucket_name" { description = "The name to apply to the bucket. Will default to a string of the function name." } +variable "create_bucket" { + type = bool + default = true + description = "Whether to create a new bucket or use an existing one. If false, `bucket_name` should reference the name of the alternate bucket to use." +} + variable "bucket_force_destroy" { type = bool default = false