Skip to content

Commit

Permalink
Support GCS objects in cloud function modules bundles (GoogleCloudPla…
Browse files Browse the repository at this point in the history
…tform#2361)

* cloud function v2

* cloud function v1

* blueprints
  • Loading branch information
ludoo authored and Alberto Geniola committed Jun 19, 2024
1 parent 808046d commit 1529a14
Show file tree
Hide file tree
Showing 19 changed files with 229 additions and 187 deletions.
3 changes: 1 addition & 2 deletions blueprints/apigee/apigee-x-foundations/monitoring.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ module "instance_monitor_function" {
bucket_config = {
}
bundle_config = {
path = "${path.module}/functions/instance-monitor"
output_path = "bundle.zip"
path = "${path.module}/functions/instance-monitor"
}
function_config = {
entry_point = "writeMetric"
Expand Down
8 changes: 2 additions & 6 deletions blueprints/apigee/bigquery-analytics/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,7 @@ module "function_export" {
lifecycle_delete_age = 1
}
bundle_config = {
path = "${path.module}/functions/export"
output_path = "${path.module}/bundle-export.zip"
excludes = null
path = "${path.module}/functions/export"
}
function_config = {
entry_point = "export"
Expand Down Expand Up @@ -200,9 +198,7 @@ module "function_gcs2bq" {
lifecycle_delete_age = 1
}
bundle_config = {
path = "${path.module}/functions/gcs2bq"
output_path = "${path.module}/bundle-gcs2bq.zip"
excludes = null
path = "${path.module}/functions/gcs2bq"
}
function_config = {
entry_point = "gcs2bq"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ module "cf" {
location = var.region
}
bundle_config = {
path = "${path.module}/cf"
output_path = var.bundle_path
path = "${path.module}/cf"
folder_options = {
archive_path = var.bundle_path
}
}
service_account = module.service-account.email
trigger_config = {
Expand Down
6 changes: 4 additions & 2 deletions blueprints/cloud-operations/compute-quota-monitoring/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ module "cf" {
location = var.region
}
bundle_config = {
path = "${path.module}/src"
output_path = var.bundle_path
path = "${path.module}/src"
folder_options = {
archive_path = var.bundle_path
}
}
service_account_create = true
trigger_config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ module "cloud-function" {
}
build_worker_pool = var.cloud_function_config.build_worker_pool_id
bundle_config = {
path = var.cloud_function_config.source_dir
output_path = var.cloud_function_config.bundle_path
path = var.cloud_function_config.source_dir
folder_options = {
archive_path = var.cloud_function_config.bundle_path
}
}
environment_variables = (
var.cloud_function_config.debug != true ? {} : { DEBUG = "1" }
Expand Down Expand Up @@ -145,8 +147,10 @@ module "cloud-function-v2" {
}
build_worker_pool = var.cloud_function_config.build_worker_pool_id
bundle_config = {
path = var.cloud_function_config.source_dir
output_path = var.cloud_function_config.bundle_path
path = var.cloud_function_config.source_dir
folder_options = {
archive_path = var.cloud_function_config.bundle_path
}
}
environment_variables = (
var.cloud_function_config.debug != true ? {} : { DEBUG = "1" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ module "cf" {
location = var.region
}
bundle_config = {
path = "${path.module}/cf"
output_path = var.bundle_path
path = "${path.module}/cf"
folder_options = {
archive_path = var.bundle_path
}
}
service_account = module.service-account.email
trigger_config = {
Expand All @@ -116,9 +118,10 @@ module "cffile" {
lifecycle_delete_age_days = null
}
bundle_config = {
path = "${path.module}/cffile"
output_path = var.bundle_path_cffile
excludes = null
path = "${path.module}/cffile"
folder_options = {
archive_path = var.bundle_path_cffile
}
}
service_account = module.service-account.email
trigger_config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ module "cf-restarter" {
location = var.region
}
bundle_config = {
path = "${path.module}/function/restarter"
output_path = "restarter.zip"
path = "${path.module}/function/restarter"
}
service_account = module.service-account-restarter.email

Expand All @@ -145,8 +144,7 @@ module "cf-healthchecker" {
region = var.region
bucket_name = module.cf-restarter.bucket_name
bundle_config = {
path = "${path.module}/function/healthchecker"
output_path = "healthchecker.zip"
path = "${path.module}/function/healthchecker"
}
service_account = module.service-account-healthchecker.email
function_config = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ module "function-hello" {
bucket_name = "${var.name}-tf-cf-deploy"
ingress_settings = "ALLOW_INTERNAL_ONLY"
bundle_config = {
path = "${path.module}/assets"
output_path = "bundle.zip"
path = "${path.module}/assets"
}
bucket_config = {
location = var.region
Expand Down
92 changes: 43 additions & 49 deletions modules/cloud-function-v1/README.md

Large diffs are not rendered by default.

43 changes: 26 additions & 17 deletions modules/cloud-function-v1/bundle.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
*/

locals {
bundle = {
name = try(
"bundle-${data.archive_file.bundle[0].output_md5}.zip",
basename(var.bundle_config.path)
bundle_type = (
startswith(var.bundle_config.path, "gs://")
? "gcs"
: (
try(fileexists(pathexpand(var.bundle_config.path)), null) != null &&
endswith(var.bundle_config.path, ".zip")
? "local-file"
: "local-folder"
)
path = try(
data.archive_file.bundle[0].output_path,
pathexpand(var.bundle_config.path)
)
}
)
}

resource "google_storage_bucket" "bucket" {
Expand Down Expand Up @@ -59,20 +59,29 @@ resource "google_storage_bucket" "bucket" {
# compress bundle in a zip archive if it's a folder

data "archive_file" "bundle" {
count = (
try(fileexists(pathexpand(var.bundle_config.path)), null) == null ? 1 : 0
count = local.bundle_type == "local-folder" ? 1 : 0
type = "zip"
source_dir = pathexpand(var.bundle_config.path)
output_path = (
var.bundle_config.folder_options.archive_path != null
? pathexpand(var.bundle_config.folder_options.archive_path)
: "/tmp/bundle-${var.project_id}-${var.name}.zip"
)
type = "zip"
source_dir = pathexpand(var.bundle_config.path)
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
excludes = var.bundle_config.folder_options.excludes
}

# upload to GCS

resource "google_storage_bucket_object" "bundle" {
name = local.bundle.name
count = local.bundle_type != "gcs" ? 1 : 0
name = try(
"bundle-${data.archive_file.bundle[0].output_md5}.zip",
basename(var.bundle_config.path)
)
bucket = local.bucket
source = local.bundle.path
source = try(
data.archive_file.bundle[0].output_path,
pathexpand(var.bundle_config.path)
)
}
30 changes: 17 additions & 13 deletions modules/cloud-function-v1/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,23 @@ resource "google_vpc_access_connector" "connector" {
}

resource "google_cloudfunctions_function" "function" {
project = var.project_id
region = var.region
name = "${local.prefix}${var.name}"
description = var.description
runtime = var.function_config.runtime
available_memory_mb = var.function_config.memory_mb
max_instances = var.function_config.instance_count
timeout = var.function_config.timeout_seconds
entry_point = var.function_config.entry_point
environment_variables = var.environment_variables
service_account_email = local.service_account_email
source_archive_bucket = local.bucket
source_archive_object = google_storage_bucket_object.bundle.name
project = var.project_id
region = var.region
name = "${local.prefix}${var.name}"
description = var.description
runtime = var.function_config.runtime
available_memory_mb = var.function_config.memory_mb
max_instances = var.function_config.instance_count
timeout = var.function_config.timeout_seconds
entry_point = var.function_config.entry_point
environment_variables = var.environment_variables
service_account_email = local.service_account_email
source_archive_bucket = local.bucket
source_archive_object = (
local.bundle_type == "gcs"
? var.bundle_config.path
: google_storage_bucket_object.bundle[0].name
)
labels = var.labels
trigger_http = var.trigger_config == null ? true : null
https_trigger_security_level = var.https_security_level == null ? "SECURE_ALWAYS" : var.https_security_level
Expand Down
26 changes: 19 additions & 7 deletions modules/cloud-function-v1/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,35 @@ variable "build_worker_pool" {
}

variable "bundle_config" {
description = "Cloud function source. If path points to a .zip archive it is uploaded as-is, otherwise an archive is created on the fly. A null output path will use a unique name for the bundle in /tmp."
description = "Cloud function source. Path can point to a GCS object URI, or a local path. A local path to a zip archive will generate a GCS object using its basename, a folder will be zipped and the GCS object name inferred when not specified."
type = object({
path = string
excludes = optional(list(string))
output_path = optional(string)
path = string
folder_options = optional(object({
archive_path = optional(string)
excludes = optional(list(string))
}), {})
})
nullable = false
validation {
condition = (
var.bundle_config.path != null && (
# GCS object
(
try(fileexists(pathexpand(var.bundle_config.path)), null) != null &&
startswith(var.bundle_config.path, "gs://") &&
endswith(var.bundle_config.path, ".zip")
) ||
)
||
# local folder
length(fileset(pathexpand(var.bundle_config.path), "**/*")) > 0
||
# local ZIP archive
(
try(fileexists(pathexpand(var.bundle_config.path)), null) != null &&
endswith(var.bundle_config.path, ".zip")
)
)
)
error_message = "Bundle path must be set to a local folder or zip file."
error_message = "Bundle path must be set to a GCS object URI, a local folder or a local zip file."
}
}

Expand Down
Loading

0 comments on commit 1529a14

Please sign in to comment.