diff --git a/modules/cloud-function/README.md b/modules/cloud-function/README.md index ebf300fccf..4983f27f1d 100644 --- a/modules/cloud-function/README.md +++ b/modules/cloud-function/README.md @@ -218,6 +218,35 @@ module "cf-http" { } } # tftest modules=1 resources=2 +``` + +### Multiple Cloud Functions within project + +When deploying multiple functions do not reuse `bundle_config.output_path` between instances as the result is undefined. Default `output_path` creates file in `/tmp` folder using project Id and function name to avoid name conflicts. + +```hcl +module "cf-http-one" { + source = "./fabric/modules/cloud-function" + project_id = "my-project" + name = "test-cf-http-one" + bucket_name = "test-cf-bundles" + bundle_config = { + source_dir = "fabric/assets" + } +} + +module "cf-http-two" { + source = "./fabric/modules/cloud-function" + project_id = "my-project" + name = "test-cf-http-two" + bucket_name = "test-cf-bundles" + bundle_config = { + source_dir = "fabric/assets" + } +} +# tftest modules=2 resources=4 inventory=multiple_functions.yaml + + ``` @@ -226,7 +255,7 @@ module "cf-http" { | name | description | type | required | default | |---|---|:---:|:---:|:---:| | [bucket_name](variables.tf#L26) | Name of the bucket that will be used for the function code. It will be created with prefix prepended if bucket_config is not null. | string | ✓ | | -| [bundle_config](variables.tf#L37) | Cloud function source folder and generated zip bundle paths. Output path defaults to '/tmp/bundle.zip' if null. | object({…}) | ✓ | | +| [bundle_config](variables.tf#L37) | Cloud function source folder and generated zip bundle paths. Output path defaults to '/tmp/bundle.zip' if null. | object({…}) | ✓ | | | [name](variables.tf#L94) | Name used for cloud function and associated resources. | string | ✓ | | | [project_id](variables.tf#L109) | Project id used for all resources. | string | ✓ | | | [bucket_config](variables.tf#L17) | Enable and configure auto-created bucket. Set fields to null to use defaults. | object({…}) | | null | diff --git a/modules/cloud-function/main.tf b/modules/cloud-function/main.tf index 35dba72942..b5a861b2a5 100644 --- a/modules/cloud-function/main.tf +++ b/modules/cloud-function/main.tf @@ -267,8 +267,8 @@ resource "google_storage_bucket_object" "bundle" { data "archive_file" "bundle" { type = "zip" source_dir = var.bundle_config.source_dir - output_path = var.bundle_config.output_path - output_file_mode = "0666" + output_path = coalesce(var.bundle_config.output_path, "/tmp/bundle-${var.project_id}-${var.name}.zip") + output_file_mode = "0644" excludes = var.bundle_config.excludes } diff --git a/modules/cloud-function/variables.tf b/modules/cloud-function/variables.tf index 97a6217a6f..5f4b28c94b 100644 --- a/modules/cloud-function/variables.tf +++ b/modules/cloud-function/variables.tf @@ -38,7 +38,7 @@ variable "bundle_config" { description = "Cloud function source folder and generated zip bundle paths. Output path defaults to '/tmp/bundle.zip' if null." type = object({ source_dir = string - output_path = optional(string, "/tmp/bundle.zip") + output_path = optional(string) excludes = optional(list(string)) }) } diff --git a/tests/modules/cloud_function/examples/multiple_functions.yaml b/tests/modules/cloud_function/examples/multiple_functions.yaml new file mode 100644 index 0000000000..b9956db5b6 --- /dev/null +++ b/tests/modules/cloud_function/examples/multiple_functions.yaml @@ -0,0 +1,25 @@ +# Copyright 2023 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +values: + module.cf-http-one.google_storage_bucket_object.bundle: + source: /tmp/bundle-my-project-test-cf-http-one.zip + module.cf-http-two.google_storage_bucket_object.bundle: + source: /tmp/bundle-my-project-test-cf-http-two.zip + +counts: + google_cloudfunctions_function: 2 + google_storage_bucket_object: 2 + modules: 2 + resources: 4