diff --git a/modules/cloud-function-v1/README.md b/modules/cloud-function-v1/README.md index ca432119df..76a551803e 100644 --- a/modules/cloud-function-v1/README.md +++ b/modules/cloud-function-v1/README.md @@ -296,28 +296,28 @@ module "cf-http" { |---|---|:---:|:---:|:---:| | [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#L44) | 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. | object({…}) | ✓ | | -| [name](variables.tf#L115) | Name used for cloud function and associated resources. | string | ✓ | | -| [project_id](variables.tf#L130) | Project id used for all resources. | string | ✓ | | -| [region](variables.tf#L135) | Region used for all resources. | string | ✓ | | +| [name](variables.tf#L127) | Name used for cloud function and associated resources. | string | ✓ | | +| [project_id](variables.tf#L142) | Project id used for all resources. | string | ✓ | | +| [region](variables.tf#L147) | Region 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 | | [build_environment_variables](variables.tf#L32) | A set of key/value environment variable pairs available during build time. | map(string) | | {} | | [build_worker_pool](variables.tf#L38) | Build worker pool, in projects//locations//workerPools/ format. | string | | null | -| [description](variables.tf#L53) | Optional description. | string | | "Terraform managed." | -| [environment_variables](variables.tf#L59) | Cloud function environment variables. | map(string) | | {} | -| [function_config](variables.tf#L65) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout. | object({…}) | | {…} | -| [https_security_level](variables.tf#L85) | The security level for the function: Allowed values are SECURE_ALWAYS, SECURE_OPTIONAL. | string | | null | -| [iam](variables.tf#L91) | IAM bindings for topic in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | -| [ingress_settings](variables.tf#L97) | Control traffic that reaches the cloud function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY . | string | | null | -| [kms_key](variables.tf#L103) | Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources in key id format. If specified, you must also provide an artifact registry repository using the docker_repository field that was created with the same KMS crypto key. | string | | null | -| [labels](variables.tf#L109) | Resource labels. | map(string) | | {} | -| [prefix](variables.tf#L120) | Optional prefix used for resource names. | string | | null | -| [repository_settings](variables.tf#L140) | Docker Registry to use for storing the function's Docker images and specific repository. If kms_key is provided, the repository must have already been encrypted with the key. | object({…}) | | {…} | -| [secrets](variables.tf#L151) | Secret Manager secrets. Key is the variable name or mountpoint, volume versions are in version:path format. | map(object({…})) | | {} | -| [service_account](variables.tf#L163) | Service account email. Unused if service account is auto-created. | string | | null | -| [service_account_create](variables.tf#L169) | Auto-create service account. | bool | | false | -| [trigger_config](variables.tf#L175) | Function trigger configuration. Leave null for HTTP trigger. | object({…}) | | null | -| [vpc_connector](variables.tf#L185) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | object({…}) | | null | -| [vpc_connector_config](variables.tf#L195) | VPC connector network configuration. Must be provided if new VPC connector is being created. | object({…}) | | null | +| [description](variables.tf#L65) | Optional description. | string | | "Terraform managed." | +| [environment_variables](variables.tf#L71) | Cloud function environment variables. | map(string) | | {} | +| [function_config](variables.tf#L77) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout. | object({…}) | | {…} | +| [https_security_level](variables.tf#L97) | The security level for the function: Allowed values are SECURE_ALWAYS, SECURE_OPTIONAL. | string | | null | +| [iam](variables.tf#L103) | IAM bindings for topic in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | +| [ingress_settings](variables.tf#L109) | Control traffic that reaches the cloud function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY . | string | | null | +| [kms_key](variables.tf#L115) | Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources in key id format. If specified, you must also provide an artifact registry repository using the docker_repository field that was created with the same KMS crypto key. | string | | null | +| [labels](variables.tf#L121) | Resource labels. | map(string) | | {} | +| [prefix](variables.tf#L132) | Optional prefix used for resource names. | string | | null | +| [repository_settings](variables.tf#L152) | Docker Registry to use for storing the function's Docker images and specific repository. If kms_key is provided, the repository must have already been encrypted with the key. | object({…}) | | {…} | +| [secrets](variables.tf#L163) | Secret Manager secrets. Key is the variable name or mountpoint, volume versions are in version:path format. | map(object({…})) | | {} | +| [service_account](variables.tf#L175) | Service account email. Unused if service account is auto-created. | string | | null | +| [service_account_create](variables.tf#L181) | Auto-create service account. | bool | | false | +| [trigger_config](variables.tf#L187) | Function trigger configuration. Leave null for HTTP trigger. | object({…}) | | null | +| [vpc_connector](variables.tf#L197) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | object({…}) | | null | +| [vpc_connector_config](variables.tf#L207) | VPC connector network configuration. Must be provided if new VPC connector is being created. | object({…}) | | null | ## Outputs diff --git a/modules/cloud-function-v1/bundle.tf b/modules/cloud-function-v1/bundle.tf index 8843672a82..1d046404ed 100644 --- a/modules/cloud-function-v1/bundle.tf +++ b/modules/cloud-function-v1/bundle.tf @@ -22,7 +22,7 @@ locals { ) path = try( data.archive_file.bundle[0].output_path, - var.bundle_config.path + pathexpand(var.bundle_config.path) ) } } @@ -56,19 +56,23 @@ resource "google_storage_bucket" "bucket" { } } -resource "google_storage_bucket_object" "bundle" { - name = local.bundle.name - bucket = local.bucket - source = local.bundle.path -} +# compress bundle in a zip archive if it's a folder data "archive_file" "bundle" { count = ( - try(fileexists(var.bundle_config.path), null) == null ? 1 : 0 + try(fileexists(pathexpand(var.bundle_config.path)), null) == null ? 1 : 0 ) type = "zip" - source_dir = var.bundle_config.path + 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 } + +# upload to GCS + +resource "google_storage_bucket_object" "bundle" { + name = local.bundle.name + bucket = local.bucket + source = local.bundle.path +} diff --git a/modules/cloud-function-v1/variables.tf b/modules/cloud-function-v1/variables.tf index a2c192140f..5dde7ad388 100644 --- a/modules/cloud-function-v1/variables.tf +++ b/modules/cloud-function-v1/variables.tf @@ -48,6 +48,18 @@ variable "bundle_config" { excludes = optional(list(string)) output_path = optional(string) }) + validation { + condition = ( + var.bundle_config.path != null && ( + ( + try(fileexists(pathexpand(var.bundle_config.path)), null) != null && + endswith(var.bundle_config.path, ".zip") + ) || + length(fileset(pathexpand(var.bundle_config.path), "**/*")) > 0 + ) + ) + error_message = "Bundle path must be set to a local folder or zip file." + } } variable "description" { diff --git a/modules/cloud-function-v2/README.md b/modules/cloud-function-v2/README.md index f8bd9ead85..1e31db6830 100644 --- a/modules/cloud-function-v2/README.md +++ b/modules/cloud-function-v2/README.md @@ -288,26 +288,26 @@ module "cf-http" { |---|---|:---:|:---:|:---:| | [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#L38) | 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. | object({…}) | ✓ | | -| [name](variables.tf#L109) | Name used for cloud function and associated resources. | string | ✓ | | -| [project_id](variables.tf#L124) | Project id used for all resources. | string | ✓ | | -| [region](variables.tf#L129) | Region used for all resources. | string | ✓ | | +| [name](variables.tf#L121) | Name used for cloud function and associated resources. | string | ✓ | | +| [project_id](variables.tf#L136) | Project id used for all resources. | string | ✓ | | +| [region](variables.tf#L141) | Region 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 | | [build_worker_pool](variables.tf#L32) | Build worker pool, in projects//locations//workerPools/ format. | string | | null | -| [description](variables.tf#L47) | Optional description. | string | | "Terraform managed." | -| [docker_repository_id](variables.tf#L53) | User managed repository created in Artifact Registry. | string | | null | -| [environment_variables](variables.tf#L59) | Cloud function environment variables. | map(string) | | {} | -| [function_config](variables.tf#L65) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout. | object({…}) | | {…} | -| [iam](variables.tf#L85) | IAM bindings for topic in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | -| [ingress_settings](variables.tf#L91) | Control traffic that reaches the cloud function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY . | string | | null | -| [kms_key](variables.tf#L97) | Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources in key id format. If specified, you must also provide an artifact registry repository using the docker_repository_id field that was created with the same KMS crypto key. | string | | null | -| [labels](variables.tf#L103) | Resource labels. | map(string) | | {} | -| [prefix](variables.tf#L114) | Optional prefix used for resource names. | string | | null | -| [secrets](variables.tf#L134) | Secret Manager secrets. Key is the variable name or mountpoint, volume versions are in version:path format. | map(object({…})) | | {} | -| [service_account](variables.tf#L146) | Service account email. Unused if service account is auto-created. | string | | null | -| [service_account_create](variables.tf#L152) | Auto-create service account. | bool | | false | -| [trigger_config](variables.tf#L158) | Function trigger configuration. Leave null for HTTP trigger. | object({…}) | | null | -| [vpc_connector](variables.tf#L176) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | object({…}) | | null | -| [vpc_connector_config](variables.tf#L186) | VPC connector network configuration. Must be provided if new VPC connector is being created. | object({…}) | | null | +| [description](variables.tf#L59) | Optional description. | string | | "Terraform managed." | +| [docker_repository_id](variables.tf#L65) | User managed repository created in Artifact Registry. | string | | null | +| [environment_variables](variables.tf#L71) | Cloud function environment variables. | map(string) | | {} | +| [function_config](variables.tf#L77) | Cloud function configuration. Defaults to using main as entrypoint, 1 instance with 256MiB of memory, and 180 second timeout. | object({…}) | | {…} | +| [iam](variables.tf#L97) | IAM bindings for topic in {ROLE => [MEMBERS]} format. | map(list(string)) | | {} | +| [ingress_settings](variables.tf#L103) | Control traffic that reaches the cloud function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY . | string | | null | +| [kms_key](variables.tf#L109) | Resource name of a KMS crypto key (managed by the user) used to encrypt/decrypt function resources in key id format. If specified, you must also provide an artifact registry repository using the docker_repository_id field that was created with the same KMS crypto key. | string | | null | +| [labels](variables.tf#L115) | Resource labels. | map(string) | | {} | +| [prefix](variables.tf#L126) | Optional prefix used for resource names. | string | | null | +| [secrets](variables.tf#L146) | Secret Manager secrets. Key is the variable name or mountpoint, volume versions are in version:path format. | map(object({…})) | | {} | +| [service_account](variables.tf#L158) | Service account email. Unused if service account is auto-created. | string | | null | +| [service_account_create](variables.tf#L164) | Auto-create service account. | bool | | false | +| [trigger_config](variables.tf#L170) | Function trigger configuration. Leave null for HTTP trigger. | object({…}) | | null | +| [vpc_connector](variables.tf#L188) | VPC connector configuration. Set create to 'true' if a new connector needs to be created. | object({…}) | | null | +| [vpc_connector_config](variables.tf#L198) | VPC connector network configuration. Must be provided if new VPC connector is being created. | object({…}) | | null | ## Outputs diff --git a/modules/cloud-function-v2/bundle.tf b/modules/cloud-function-v2/bundle.tf index 8843672a82..1d046404ed 100644 --- a/modules/cloud-function-v2/bundle.tf +++ b/modules/cloud-function-v2/bundle.tf @@ -22,7 +22,7 @@ locals { ) path = try( data.archive_file.bundle[0].output_path, - var.bundle_config.path + pathexpand(var.bundle_config.path) ) } } @@ -56,19 +56,23 @@ resource "google_storage_bucket" "bucket" { } } -resource "google_storage_bucket_object" "bundle" { - name = local.bundle.name - bucket = local.bucket - source = local.bundle.path -} +# compress bundle in a zip archive if it's a folder data "archive_file" "bundle" { count = ( - try(fileexists(var.bundle_config.path), null) == null ? 1 : 0 + try(fileexists(pathexpand(var.bundle_config.path)), null) == null ? 1 : 0 ) type = "zip" - source_dir = var.bundle_config.path + 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 } + +# upload to GCS + +resource "google_storage_bucket_object" "bundle" { + name = local.bundle.name + bucket = local.bucket + source = local.bundle.path +} diff --git a/modules/cloud-function-v2/variables.tf b/modules/cloud-function-v2/variables.tf index 481de85528..c37e8d804c 100644 --- a/modules/cloud-function-v2/variables.tf +++ b/modules/cloud-function-v2/variables.tf @@ -42,6 +42,18 @@ variable "bundle_config" { excludes = optional(list(string)) output_path = optional(string) }) + validation { + condition = ( + var.bundle_config.path != null && ( + ( + try(fileexists(pathexpand(var.bundle_config.path)), null) != null && + endswith(var.bundle_config.path, ".zip") + ) || + length(fileset(pathexpand(var.bundle_config.path), "**/*")) > 0 + ) + ) + error_message = "Bundle path must be set to a local folder or zip file." + } } variable "description" {