|
| 1 | +# Guide: Cloud Functions (Cloud Run Functions) |
| 2 | + |
| 3 | + > This guide explains the functionality of Cloud Functions and how to control their behavior. While this knowledge is not required to deploy the FortiGate autoscale project, understanding it can help with debugging and customizing behaviors. |
| 4 | +
|
| 5 | +Google Cloud Functions (also known as Cloud Run Functions) is a serverless, event-driven computing service that allows you to run code without managing servers. This project uses Cloud Functions for FGT license management, configuration setup, and autoscaling. |
| 6 | + |
| 7 | +The Google Cloud Functions is used by the `fgt_asg_with_function` module. Its script is stored in `/modules/fortigate/fgt_asg_with_function/cloud_function.zip`. You can download the latest file [here](https://github.com/fortinetdev/terraform-google-cloud-modules/blob/main/modules/fortigate/fgt_asg_with_function/cloud_function.zip). |
| 8 | + |
| 9 | + |
| 10 | +- [Guide: Cloud Functions (Cloud Run Functions)](#guide-cloud-functions-cloud-run-functions) |
| 11 | + - [Workflow](#workflow) |
| 12 | + - [Customize Function Behaviors](#customize-function-behaviors) |
| 13 | + - [Cloud Functions Service](#cloud-functions-service) |
| 14 | + - [Logging](#logging) |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Workflow |
| 19 | + |
| 20 | + |
| 21 | +Google Cloud Functions is an event-driven computing service, typically triggered by the creation or deletion of FortiGate (FGT) instances. |
| 22 | + |
| 23 | +When an FGT instance is created or deleted, a corresponding [log](https://console.cloud.google.com/logs/query) is generated. This Terraform project includes a [log router sink](https://console.cloud.google.com/logs/router) that continuously monitors Google Cloud Logs. When the log router sink detects an FGT creation or deletion event, it forwards a message to a [Pub/Sub topic](https://console.cloud.google.com/cloudpubsub/topic/list), which in turn triggers the execution of [Cloud Functions](https://console.cloud.google.com/run). |
| 24 | + |
| 25 | +Cloud Functions operate as multi-threaded Python scripts, with each event triggering a separate thread. These scripts handle tasks such as FGT license management, configuration setup, and autoscaling. Cloud Functions interact with the [Firestroe database](https://console.cloud.google.com/firestore/databases) for data storage and retrieval. If a task cannot be completed within the current thread, the Cloud Function thread saves the task details to Firestore and publishes a message to the pub/sub topic. This triggers another Cloud Function thread to process the remaining tasks, ensuring seamless execution and scalability. |
| 26 | + |
| 27 | +## Customize Function Behaviors |
| 28 | + |
| 29 | +All examples (e.g., `"fortinetdev/cloud-modules/google//examples/autoscale_fgt_as_hub"`) and modules (e.g., `"fortinetdev/cloud-modules/google//modules/fortigate/fgt_asg_with_function"`) that rely on the Cloud Function include an input variable `cloud_function`, which defines its behavior. |
| 30 | + |
| 31 | + |
| 32 | +### Cloud Functions Service |
| 33 | + |
| 34 | +Cloud Functions are running service with configurable parameters defined in `cloud_function->service_config`. Every example provides default values for `cloud_function->service_config`, which are typically sufficient for its specific use case. However, you can adjust these values to better fit your needs. |
| 35 | + |
| 36 | +The following parameters can be customized: |
| 37 | +- `max_instance_count` – The maximum number of function instances that can run concurrently. |
| 38 | +- `max_instance_request_concurrency` – The maximum number of concurrent requests a single Cloud Function instance can handle. |
| 39 | +- `available_cpu` – The number of CPUs allocated per function instance. |
| 40 | +- `available_memory` – The amount of memory available for each function instance. |
| 41 | +- `timeout_seconds` – The maximum execution time for the function. |
| 42 | + |
| 43 | +**Example Configuration:** |
| 44 | +```hcl |
| 45 | +cloud_function = { |
| 46 | + # <other parameters...> |
| 47 | + service_config = { |
| 48 | + max_instance_count = 1 # Maximum number of concurrent function instances. |
| 49 | + max_instance_request_concurrency = 3 # Maximum concurrent requests handled per instance. |
| 50 | + available_cpu = "1" # The number of CPUs used in a single container instance. |
| 51 | + available_memory = "1G" # The amount of memory available for a function. |
| 52 | + timeout_seconds = 420 # The function execution timeout. |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +### Logging |
| 58 | + |
| 59 | +This project categorizes logs into five levels: `"ERROR"`, `"WARN"`, `"INFO"`, `"DEBUG"`, and `"TRACE"`. |
| 60 | + |
| 61 | +**Type of Log** |
| 62 | +- `"ERROR"`: A critical issue occurred in the Cloud Function that may impact functionality. |
| 63 | +- `"WARN"`: Unexpected behavior detected, but functionality remains unaffected. |
| 64 | +- `"INFO"`: General project progress updates, such as Cloud Function execution start and completion. |
| 65 | +- `"DEBUG"`: Debugging information, which may contain sensitive data. |
| 66 | +- `"TRACE"`: Highly detailed logs, capturing all available information, including sensitive data. |
| 67 | + |
| 68 | +You can view logs in the [Log Explorer](https://console.cloud.google.com/logs/query). |
| 69 | + |
| 70 | +**Configuring Log Levels** |
| 71 | + |
| 72 | +The logging level is controlled by the `cloud_function->logging_level` parameter. |
| 73 | +- `"NONE"`: No logs recorded. |
| 74 | +- `"ERROR"`: Logs only "ERROR" events. |
| 75 | +- `"WARN"`: Logs "WARN" and "ERROR" events. |
| 76 | +- `"INFO"`: Logs "INFO", "WARN", and "ERROR" events. |
| 77 | +- `"DEBUG"`: Logs "DEBUG", "INFO", "WARN", and ERROR events. |
| 78 | +- `"TRACE"`: Logs all events ("ERROR", "WARN", "INFO", "DEBUG", and "TRACE"). |
| 79 | + |
| 80 | +**Example Configuration:** |
| 81 | +```hcl |
| 82 | +cloud_function = { |
| 83 | + # <other parameters...> |
| 84 | + service_config = { |
| 85 | + logging_level = "TRACE" # Verbosity of logs. Possible values include "NONE", "ERROR", "WARN", "INFO", "DEBUG", and "TRACE". You can find logs in Google Cloud Logs Explorer. |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | + |
0 commit comments