A Terraform module for deploying Google Cloud Functions Gen 2 triggered by Pub/Sub messages with automatic source code packaging and deployment.
- Automatic source code zipping and upload to GCS
- Cloud Function Gen 2 deployment with Pub/Sub trigger
- Lifecycle management for old function archives
- Configurable runtime, memory, timeout, and scaling
- Environment variable support
- Built-in retry policy configuration
The project assumes the following:
- A basic understanding of Git.
- Git version
>= 2.33.0. - An existing GCP IAM user or role with access to create/update/delete resources defined in main.tf.
- GCloud CLI
>= 465.0.0. - A basic understanding of Terraform.
- Terraform version
>= 1.3.0. - (Optional - for local testing) A basic understanding of Make.
- Make version
>= GNU Make 3.81. - Important Note: This project includes a Makefile to speed up local development in Terraform. The
maketargets act as a wrapper around Terraform commands. As such,makehas only been tested/verified on Linux/Mac OS. Though, it is possible to install make using Chocolatey, we do not guarantee this approach as it has not been tested/verified. You may use the commands in the Makefile as a guide to run each Terraform command locally on Windows.
- Make version
Important Note: This project includes a Makefile to speed up local development in Terraform. The make targets act as a wrapper around Terraform commands. As such, make has only been tested/verified on Linux/Mac OS. Though, it is possible to install make using Chocolatey, we do not guarantee this approach as it has not been tested/verified. You may use the commands in the Makefile as a guide to run each Terraform command locally on Windows.
gcloud init # https://cloud.google.com/docs/authentication/gcloud
gcloud auth application-default login
# Copy the example tfvars and customize it
cp examples/simple/examples.tfvars examples/simple/terraform.tfvars
# Edit terraform.tfvars with your values
# Run terraform commands
make plan
make apply
make destroyContributions are always welcome. As such, this project uses the main branch as the source of truth to track changes.
Step 1. Clone this project.
# Using SSH
$ git clone git@github.com:nurdsoft/terraform-google-cloud-function.git
# Using HTTPS
$ git clone https://github.com/nurdsoft/terraform-google-cloud-function.gitStep 2. Checkout a feature branch: git checkout -b feature/abc.
Step 3. Validate the change/s locally by executing the steps defined under Test.
Step 4. If testing is successful, commit and push the new change/s to the remote.
$ git add file1 file2 ...
$ git commit -m "Adding some change"
$ git push --set-upstream origin feature/abcStep 5. Once pushed, create a PR and assign it to a member for review.
- Important Note: It can be helpful to attach the
terraform planoutput in the PR.
Step 6. A team member reviews/approves/merges the change/s.
Step 7. Once merged, deploy the required changes as needed.
Step 8. Once deployed, verify that the changes have been deployed.
- If possible, please add a
planoutput using the feature branch so the member reviewing the PR has better visibility into the changes.
module "cloud_function" {
source = "git::https://github.com/nurdsoft/terraform-google-cloud-function.git?ref=main"
project_id = "my-project"
region = "us-central1"
function_source_dir = "./my-function"
function_entry_point = "HandleMessage"
pubsub_topic_id = "projects/my-project/topics/my-topic"
environment_variables = {
SLACK_WEBHOOK_URL = var.slack_webhook_url
}
}| Example | Description |
|---|---|
| simple | Minimal setup with a sample Go function |
| Name | Version |
|---|---|
| terraform | >= 1.3 |
| >= 5.0 | |
| archive | >= 2.0 |
| Name | Version |
|---|---|
| >= 5.0 | |
| archive | >= 2.0 |
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
project_id |
The GCP project ID to deploy resources into | string |
n/a | yes |
region |
The GCP region to deploy the Cloud Function and Storage Bucket | string |
n/a | yes |
function_source_dir |
The local directory path containing the Cloud Function source code to be zipped and deployed | string |
n/a | yes |
function_entry_point |
The name of the function to execute when the Cloud Function is triggered | string |
n/a | yes |
pubsub_topic_id |
The fully-qualified Pub/Sub topic ID that triggers the Cloud Function (e.g. projects/my-project/topics/my-topic) | string |
n/a | yes |
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
function_bucket_name |
The base name of the GCS bucket used to store the function source code. The project_id will be appended to ensure global uniqueness | string |
"cloud-function-source" |
no |
bucket_lifecycle_age_days |
Number of days after which old function archives are automatically deleted from the bucket | number |
30 |
no |
function_name |
The name of the Cloud Function | string |
"pubsub-triggered-function" |
no |
function_description |
The description of the Cloud Function | string |
"Cloud Function triggered by Pub/Sub messages" |
no |
function_runtime |
The runtime environment for the Cloud Function (e.g. go125, python311, nodejs20) | string |
"go125" |
no |
max_instance_count |
The maximum number of function instances that can run simultaneously | number |
3 |
no |
available_memory |
The amount of memory available for the function (e.g. 256Mi, 512Mi, 1Gi) | string |
"512Mi" |
no |
timeout_seconds |
The maximum amount of time the function can run before timing out (in seconds) | number |
60 |
no |
environment_variables |
A map of environment variables to pass to the Cloud Function | map(string) |
{} |
no |
event_trigger_type |
The type of event that triggers the function | string |
"google.cloud.pubsub.topic.v1.messagePublished" |
no |
retry_policy |
The retry policy for failed function executions (RETRY_POLICY_RETRY or RETRY_POLICY_DO_NOT_RETRY) | string |
"RETRY_POLICY_DO_NOT_RETRY" |
no |
| Name | Description |
|---|---|
function_id |
The fully-qualified ID of the Cloud Function |
function_name |
The name of the Cloud Function |
function_url |
The URL of the Cloud Function service |
bucket_name |
The name of the GCS bucket storing the function source code |
bucket_url |
The URL of the GCS bucket |
Module is maintained by Nurdsoft.
Apache 2 Licensed. See LICENSE for full details.