Skip to content

Improve scheduled function module: replace versioning with hash-based naming and add retry configuration - #4

Merged
jwbron merged 2 commits into
mainfrom
scheduled-fun-improvements
Aug 20, 2025
Merged

Improve scheduled function module: replace versioning with hash-based naming and add retry configuration#4
jwbron merged 2 commits into
mainfrom
scheduled-fun-improvements

Conversation

@jwbron

@jwbron jwbron commented Aug 20, 2025

Copy link
Copy Markdown
Contributor

Summary:

This PR improves the scheduled function Terraform module with several key enhancements:

Storage Bucket Improvements:

  • Removed object versioning and lifecycle rules in favor of hash-based object naming
  • Added force_destroy = true to allow bucket deletion during terraform destroy
  • This simplifies bucket management and reduces storage costs

Function Archive Management:

  • Changed object naming to include source code hash: ${function_name}-function-${hash}.zip
  • This ensures Cloud Functions redeploy when source code changes (new hash = new object name)
  • Terraform automatically handles cleanup of old zip files when hash changes
  • No manual lifecycle rules needed - Terraform's resource replacement handles it

Source Directory Handling:

  • Added abspath() wrapper around var.source_dir for more reliable path resolution

Retry Policy Configuration:

  • Added new retries_enabled variable (default: false) to control retry behavior
  • Made retry policy configurable: RETRY_POLICY_RETRY vs RETRY_POLICY_DO_NOT_RETRY
  • This provides better control over function execution behavior

These changes improve resource management, reduce storage costs, and provide better configurability for the scheduled function module.

Test plan:

  1. Deploy a new scheduled function using this updated module
  2. Verify function deploys correctly with hash-based naming
  3. Test source code changes trigger redeployment (new hash generated)
  4. Verify retry policy works correctly with both enabled/disabled settings
  5. Test terraform destroy completes successfully with force_destroy
  6. Verify no orphaned zip files remain in storage bucket

@jwbron jwbron self-assigned this Aug 20, 2025
@jwbron
jwbron requested a review from a team August 20, 2025 18:01
@jwbron
jwbron force-pushed the scheduled-fun-improvements branch from 9560792 to 90664ed Compare August 20, 2025 18:03

@nathanjd nathanjd left a comment

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.

Changes make sense to me!

@jwbron
jwbron merged commit 1b0e56d into main Aug 20, 2025
1 check passed
@jwbron
jwbron deleted the scheduled-fun-improvements branch August 20, 2025 18:27
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.

jwbron added a commit that referenced this pull request Jul 21, 2026
…l + write-only (#37)

## Summary

The scheduled-job module reads `Slack__API_token_for_alertlib` with a `data "google_secret_manager_secret_version"` to build each job's Slack notification channel. Terraform state stores the full data-source response, including `secret_data`, so the token value sits in every consumer's state and every saved plan file; this is how the token was exposed by the committed-tfplan incident (see Khan/actions#274 and the consumer migrations).

## Change

The channel stays fully Terraform-managed; the leak is fixed with the ephemeral/write-only mechanism (https://developer.hashicorp.com/terraform/language/manage-sensitive-data/ephemeral):

- the token is read with `ephemeral "google_secret_manager_secret_version"` (never persisted to state or plans), and
- written to the channel via the write-only `sensitive_labels.auth_token_wo` + `auth_token_wo_version` arguments, added to `google_monitoring_notification_channel` in provider 7.19.0.

Rotation becomes explicit: after adding a new secret version, bump the new `slack_token_rotation` variable; the next apply re-reads the latest version and rewrites the channel token. (The old data-source design also only picked up new versions on the next plan, so nothing is lost; the bump makes the cutover reviewable.)

An earlier revision of this PR added a `notification_channel_ids` input for pre-created channels; that is dropped in favor of keeping channel management in Terraform.

## Requirements and rollout

- Terraform >= 1.11 (write-only arguments) and hashicorp/google >= 7.19.0; the module's `required_providers` now enforces both.
- Suggest releasing as `scheduled-job-v0.4.0`.
- Consumer follow-ups (culture-cron, beep-boop, internal-webserver; five jobs total, all currently embedding the token in state): bump the module pin, and bump the google provider pin from `7.8.0` to `>= 7.19.0` in the two stacks that pin exactly (no committed lock files, so that is the whole change). The adoption plan should show the channel's sensitive labels updating in place; after apply plus a state refresh, the token value is gone from live state.
- Rotating the alertlib token (rotation runbook item #4) is still required regardless, since historical plan files already leaked it; after consumers adopt this version, the runbook's #4 mechanics change from "drift plan" to "bump slack_token_rotation in each repo".

Author: jwbron

Reviewers: nathanjd

Required Reviewers:

Approved By: nathanjd

Checks: ⏭️  1 check has been skipped

Pull Request URL: #37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants