Skip to content

Commit a6b2304

Browse files
committed
adds option to keep bucket
1 parent 9fa60be commit a6b2304

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module "localhost_function" {
5555
| bucket\_force\_destroy | When deleting the GCS bucket containing the cloud function, delete all objects in the bucket first. | bool | `"false"` | no |
5656
| bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map(string) | `<map>` | no |
5757
| bucket\_name | The name to apply to the bucket. Will default to a string of the function name. | string | `""` | no |
58+
| create\_bucket | Whether to create a new bucket or use an existing bucket with the bucket_name passed to the module. | bool | `"true"` | no |
5859
| description | The description of the function. | string | `"Processes events."` | no |
5960
| entry\_point | The name of a method in the function source which will be invoked when the function is executed. | string | n/a | yes |
6061
| environment\_variables | A set of key/value environment variable pairs to assign to the function. | map(string) | `<map>` | no |

main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@ resource "google_storage_bucket" "main" {
5555
storage_class = "REGIONAL"
5656
labels = var.bucket_labels
5757
bucket_policy_only = true
58+
count = var.create_bucket == true ? 1 : 0
5859
}
5960

6061
resource "google_storage_bucket_object" "main" {
6162
name = "${data.archive_file.main.output_md5}-${basename(data.archive_file.main.output_path)}"
62-
bucket = google_storage_bucket.main.name
63+
bucket = var.create_bucket == true ? google_storage_bucket.main[0].name : var.bucket_name
6364
source = data.archive_file.main.output_path
6465
content_disposition = "attachment"
6566
content_encoding = "gzip"
@@ -85,7 +86,7 @@ resource "google_cloudfunctions_function" "main" {
8586
labels = var.labels
8687
runtime = var.runtime
8788
environment_variables = var.environment_variables
88-
source_archive_bucket = google_storage_bucket.main.name
89+
source_archive_bucket = var.create_bucket == true ? google_storage_bucket.main[0].name : var.bucket_name
8990
source_archive_object = google_storage_bucket_object.main.name
9091
project = var.project_id
9192
region = var.region

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ variable "bucket_name" {
106106
description = "The name to apply to the bucket. Will default to a string of the function name."
107107
}
108108

109+
variable "create_bucket" {
110+
type = bool
111+
default = true
112+
description = "Whether to create a new bucket or use an existing bucket with the bucket_name passed to the module."
113+
}
114+
109115
variable "bucket_force_destroy" {
110116
type = bool
111117
default = false

0 commit comments

Comments
 (0)