Skip to content

Commit 7ec5539

Browse files
authored
feat: Add option to use an existing archive bucket (#42)
Adds option to use an existing archive bucket
2 parents 9fa60be + e966874 commit 7ec5539

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 one. If false, `bucket_name` should reference the name of the alternate bucket to use. | 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
@@ -48,6 +48,7 @@ data "archive_file" "main" {
4848
}
4949

5050
resource "google_storage_bucket" "main" {
51+
count = var.create_bucket ? 1 : 0
5152
name = coalesce(var.bucket_name, var.name)
5253
force_destroy = var.bucket_force_destroy
5354
location = var.region
@@ -59,7 +60,7 @@ resource "google_storage_bucket" "main" {
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 ? 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 ? 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 one. If false, `bucket_name` should reference the name of the alternate bucket to use."
113+
}
114+
109115
variable "bucket_force_destroy" {
110116
type = bool
111117
default = false

0 commit comments

Comments
 (0)