|
| 1 | +# terraform-scheduled-function-module |
| 2 | + |
| 3 | +A reusable Terraform module for scheduled Google Cloud Functions. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +Creates a complete scheduled function setup: |
| 8 | +- Cloud Function (2nd gen) with configurable runtime |
| 9 | +- Cloud Scheduler with cron-based scheduling |
| 10 | +- PubSub topic for reliable triggering |
| 11 | +- Service account with least-privilege permissions |
| 12 | +- Storage bucket with lifecycle management |
| 13 | +- Secret Manager IAM bindings |
| 14 | +- Source code change detection |
| 15 | + |
| 16 | +## Quick Start |
| 17 | + |
| 18 | +```hcl |
| 19 | +module "my_daily_task" { |
| 20 | + source = "git::https://github.com/Khan/terraform-scheduled-function-module.git?ref=v1.0.0" |
| 21 | +
|
| 22 | + function_name = "my-daily-task" |
| 23 | + project_id = "my-gcp-project" |
| 24 | + secrets_project_id = "my-secrets-gcp-project" |
| 25 | + source_dir = "./functions/my-task" |
| 26 | + main_file = "main.py" |
| 27 | + schedule = "0 9 * * 1-5" # 9 AM weekdays |
| 28 | + description = "My daily automated task" |
| 29 | +
|
| 30 | + environment_variables = { |
| 31 | + ENV = "production" |
| 32 | + } |
| 33 | +
|
| 34 | + secrets = [ |
| 35 | + { |
| 36 | + env_var_name = "API_TOKEN" |
| 37 | + secret_id = "my-api-token" |
| 38 | + version = "latest" |
| 39 | + } |
| 40 | + ] |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Examples |
| 45 | + |
| 46 | +Complete working examples are available in the [`examples/`](./examples/) directory: |
| 47 | + |
| 48 | +- **[`simple-function/`](./examples/simple-function/)** - Basic scheduled function with minimal configuration |
| 49 | + |
| 50 | +Each example includes: |
| 51 | +- Complete Terraform configuration |
| 52 | +- Sample function code with `requirements.txt` |
| 53 | +- Documentation on how to deploy and test |
| 54 | + |
| 55 | +## Cross-Repository Usage |
| 56 | + |
| 57 | +### Use Everywhere |
| 58 | +```hcl |
| 59 | +# Production: Pin to specific version |
| 60 | +module "backup" { |
| 61 | + source = "git::https://github.com/YourOrg/terraform-scheduled-function-module.git?ref=v1.0.0" |
| 62 | + # ... config |
| 63 | +} |
| 64 | +
|
| 65 | +# Development: Use latest |
| 66 | +module "test_function" { |
| 67 | + source = "git::https://github.com/YourOrg/terraform-scheduled-function-module.git" |
| 68 | + # ... config |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +## Usage Examples |
| 73 | + |
| 74 | +### Multiple Functions |
| 75 | +```hcl |
| 76 | +module "daily_backup" { |
| 77 | + source = "git::https://github.com/YourOrg/terraform-scheduled-function-module.git?ref=v1.0.0" |
| 78 | + |
| 79 | + function_name = "daily-backup" |
| 80 | + schedule = "0 2 * * *" # 2 AM daily |
| 81 | + source_dir = "./functions/backup" |
| 82 | + main_file = "backup.py" |
| 83 | + # ... other config |
| 84 | +} |
| 85 | +
|
| 86 | +module "weekly_reports" { |
| 87 | + source = "git::https://github.com/YourOrg/terraform-scheduled-function-module.git?ref=v1.0.0" |
| 88 | + |
| 89 | + function_name = "weekly-reports" |
| 90 | + schedule = "0 9 * * 1" # Monday 9 AM |
| 91 | + source_dir = "./functions/reports" |
| 92 | + main_file = "reports.py" |
| 93 | + memory = "4096M" |
| 94 | + # ... other config |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Advanced Configuration |
| 99 | +```hcl |
| 100 | +module "data_processor" { |
| 101 | + source = "git::https://github.com/YourOrg/terraform-scheduled-function-module.git?ref=v1.0.0" |
| 102 | + |
| 103 | + function_name = "data-processor" |
| 104 | + project_id = var.project_id |
| 105 | + secrets_project_id = var.secrets_project_id |
| 106 | + source_dir = "./functions/processor" |
| 107 | + main_file = "processor.py" |
| 108 | + schedule = "0 */6 * * *" # Every 6 hours |
| 109 | + |
| 110 | + # Resource configuration |
| 111 | + memory = "4096M" |
| 112 | + timeout_seconds = 300 |
| 113 | + max_instance_count = 3 |
| 114 | + |
| 115 | + # Multiple secrets |
| 116 | + secrets = [ |
| 117 | + { |
| 118 | + env_var_name = "DATABASE_URL" |
| 119 | + secret_id = "postgres-connection" |
| 120 | + version = "latest" |
| 121 | + }, |
| 122 | + { |
| 123 | + env_var_name = "API_KEY" |
| 124 | + secret_id = "external-api-key" |
| 125 | + version = "2" |
| 126 | + } |
| 127 | + ] |
| 128 | +
|
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +## Requirements & Inputs |
| 133 | + |
| 134 | +### Required |
| 135 | +- `function_name` - Unique name for your function |
| 136 | +- `project_id` - GCP project for resources |
| 137 | +- `secrets_project_id` - GCP project containing secrets |
| 138 | +- `source_dir` - Path to function code |
| 139 | +- `main_file` - Python file name (e.g., "main.py"), relative to `source_dir`. |
| 140 | +- `schedule` - Cron expression (e.g., "0 9 * * 1-5") |
| 141 | +- `description` - Function description |
| 142 | + |
| 143 | +### Optional (with defaults) |
| 144 | +- `region` - GCP region ("us-central1") |
| 145 | +- `runtime` - Function runtime ("python311") |
| 146 | +- `memory` - Memory allocation ("2048M") |
| 147 | +- `timeout_seconds` - Timeout (60) |
| 148 | +- `environment_variables` - Environment vars ({}) |
| 149 | +- `secrets` - Secret Manager secrets ([]) |
| 150 | + |
| 151 | +## Outputs |
| 152 | + |
| 153 | +- `function_name` - Name of deployed function |
| 154 | +- `function_url` - Function URL |
| 155 | +- `service_account_email` - Function service account |
| 156 | +- `scheduler_job_name` - Scheduler job name |
| 157 | + |
| 158 | +## Repository Structure |
| 159 | + |
| 160 | +``` |
| 161 | +your-app-repo/ |
| 162 | +├── terraform/ |
| 163 | +│ └── main.tf # Uses module |
| 164 | +├── functions/ |
| 165 | +│ ├── daily-backup/ |
| 166 | +│ │ ├── main.py |
| 167 | +│ │ └── requirements.txt |
| 168 | +│ └── reports/ |
| 169 | +│ ├── main.py |
| 170 | +│ └── requirements.txt |
| 171 | +└── README.md |
| 172 | +``` |
| 173 | + |
| 174 | +## Function Code Structure |
| 175 | + |
| 176 | +### Required Files |
| 177 | + |
| 178 | +Your source directory must contain: |
| 179 | + |
| 180 | +``` |
| 181 | +functions/my-task/ |
| 182 | +├── main.py # Entry point function |
| 183 | +└── requirements.txt # Python dependencies (if needed) |
| 184 | +``` |
| 185 | + |
| 186 | +### Python Function Code |
| 187 | + |
| 188 | +```python |
| 189 | +# functions/my-task/main.py |
| 190 | +import functions_framework |
| 191 | + |
| 192 | +@functions_framework.cloud_event |
| 193 | +def main(cloud_event): |
| 194 | + """Function entry point""" |
| 195 | + print("Task running!") |
| 196 | + return "Success" |
| 197 | +``` |
| 198 | + |
| 199 | +### Dependencies |
| 200 | + |
| 201 | +If your function uses external packages, include a `requirements.txt` file. Cloud Functions automatically install dependencies from `requirements.txt` during deployment. No local installation is needed - the module simply packages your source code and lets Cloud Functions handle dependency management. |
| 202 | + |
| 203 | +## Common Cron Patterns |
| 204 | + |
| 205 | +| Schedule | Description | |
| 206 | +|----------|-------------| |
| 207 | +| `"0 9 * * 1-5"` | 9 AM weekdays | |
| 208 | +| `"0 */6 * * *"` | Every 6 hours | |
| 209 | +| `"0 2 * * *"` | 2 AM daily | |
| 210 | +| `"0 9 * * 1"` | Monday 9 AM | |
| 211 | +| `"*/15 * * * *"` | Every 15 minutes | |
0 commit comments