Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unique bundle name for Cloud Function #1281

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 30 additions & 1 deletion modules/cloud-function/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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


```
<!-- BEGIN TFDOC -->

Expand All @@ -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. | <code>string</code> | ✓ | |
| [bundle_config](variables.tf#L37) | Cloud function source folder and generated zip bundle paths. Output path defaults to '/tmp/bundle.zip' if null. | <code title="object&#40;&#123;&#10; source_dir &#61; string&#10; output_path &#61; optional&#40;string, &#34;&#47;tmp&#47;bundle.zip&#34;&#41;&#10; excludes &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | ✓ | |
| [bundle_config](variables.tf#L37) | Cloud function source folder and generated zip bundle paths. Output path defaults to '/tmp/bundle.zip' if null. | <code title="object&#40;&#123;&#10; source_dir &#61; string&#10; output_path &#61; optional&#40;string&#41;&#10; excludes &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | ✓ | |
| [name](variables.tf#L94) | Name used for cloud function and associated resources. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L109) | Project id used for all resources. | <code>string</code> | ✓ | |
| [bucket_config](variables.tf#L17) | Enable and configure auto-created bucket. Set fields to null to use defaults. | <code title="object&#40;&#123;&#10; location &#61; optional&#40;string&#41;&#10; lifecycle_delete_age_days &#61; optional&#40;number&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
Expand Down
4 changes: 2 additions & 2 deletions modules/cloud-function/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion modules/cloud-function/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
}
Expand Down
25 changes: 25 additions & 0 deletions tests/modules/cloud_function/examples/multiple_functions.yaml
Original file line number Diff line number Diff line change
@@ -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