Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions terraform/modules/scheduled-function/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,24 @@ resource "google_storage_bucket" "function_bucket" {
name = "${var.function_name}-source-${var.project_id}"
location = var.region
uniform_bucket_level_access = true

# Enable versioned objects
versioning {
enabled = true
}

# Keep only the 3 most recent versions of each object
lifecycle_rule {
condition {
num_newer_versions = 3
}
action {
type = "Delete"
}
}
force_destroy = true
}

# Create function source archive
data "archive_file" "function_archive" {
type = "zip"
output_path = "${path.module}/${var.function_name}-function.zip"
source_dir = var.source_dir
source_dir = abspath(var.source_dir)
excludes = var.excludes
}

# Upload function archive to storage bucket
# The object name includes the source code hash, ensuring:
# 1. Cloud Function redeploys when source code changes (new hash = new object name)
# 2. Terraform automatically deletes old zip files when hash changes (resource replacement)
# 3. No manual cleanup or lifecycle rules needed - Terraform handles it
resource "google_storage_bucket_object" "function_archive" {
name = "${var.function_name}-function.zip"
name = "${var.function_name}-function-${data.archive_file.function_archive.output_sha}.zip"
bucket = google_storage_bucket.function_bucket.name
source = data.archive_file.function_archive.output_path
}
Expand Down Expand Up @@ -146,6 +136,6 @@ resource "google_cloudfunctions2_function" "function" {
trigger_region = var.region
event_type = "google.cloud.pubsub.topic.v1.messagePublished"
pubsub_topic = google_pubsub_topic.function_topic.id
retry_policy = "RETRY_POLICY_RETRY"
retry_policy = var.retries_enabled ? "RETRY_POLICY_RETRY" : "RETRY_POLICY_DO_NOT_RETRY"
}
}
6 changes: 6 additions & 0 deletions terraform/modules/scheduled-function/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ variable "min_instance_count" {
default = 1
}

variable "retries_enabled" {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be very useful to disable for many cronjobs! Where if it fails we just wait for the next run again 5 minutes later.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's why I decided to default this to false. It should probably be disabled for most jobs.

description = "Whether the retry policy is set to `RETRY_POLICY_RETRY`"
type = bool
default = false
}

# Environment variables
variable "environment_variables" {
description = "Environment variables for the Cloud Function"
Expand Down