Skip to content

Commit f11b207

Browse files
authored
Allow scheduled cloud functions to have multiple scheduling jobs (#27)
* converted scheduler job to foreach * added attributes * added optionals * enable experimental feature * moved definition * moved scheduler sa outside of loop * turn list of objects into set * fixed conditions on non-bool values * adjusted conditions
1 parent 292e28f commit f11b207

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

terraform/cloud_function/scheduled/main.tf

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Cloud Function invoked by a scheduled job through a HTTP request
44
Often used to do things that need to be executed at regular intervals,
55
like pulling data from external sources or doing aggregations
66
*/
7+
78
resource "google_cloudfunctions_function" "function" {
89
available_memory_mb = var.function_memory
910
entry_point = var.function_entry_point
@@ -33,20 +34,21 @@ resource "google_storage_bucket_object" "functioncode" {
3334
}
3435

3536
resource "google_cloud_scheduler_job" "scheduler_job" {
36-
attempt_deadline = var.scheduler_attempt_deadline
37-
count = var.scheduler_enabled ? 1 : 0
38-
name = format("%s%s", var.function_name, substr(md5(var.branch_suffix), 0, 26))
39-
schedule = var.scheduler_schedule
37+
for_each = {for scheduler in var.schedulers: scheduler.name => scheduler}
38+
39+
attempt_deadline = each.value.attempt_deadline != null ? each.value.attempt_deadline : "320s"
40+
name = each.value.name
41+
schedule = each.value.schedule
4042
time_zone = "Europe/Amsterdam"
4143

4244
retry_config {
43-
retry_count = var.scheduler_retry_count
45+
retry_count = each.value.retry_count != null ? each.value.retry_count : 1
4446
}
4547

4648
http_target {
47-
http_method = var.scheduler_request_method
49+
body = base64encode(each.value.request_body != null ? each.value.request_body : "{}")
50+
http_method = each.value.request_method != null ? each.value.request_method : "POST"
4851
uri = google_cloudfunctions_function.function.https_trigger_url
49-
body = base64encode(var.scheduler_request_body)
5052

5153
oidc_token {
5254
service_account_email = var.scheduler_service_account_email
Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# must be defined in same file as where we define the variable with optional attrs
2+
terraform {
3+
experiments = [module_variable_optional_attrs]
4+
}
5+
16
variable "branch_suffix" {}
27
variable "function_entry_point" {
38
default = "handler"
@@ -22,24 +27,17 @@ variable "function_vpc_connector_egress_settings" {
2227
}
2328
variable "project_id" {}
2429
variable "project_region" {}
25-
variable "scheduler_attempt_deadline" {
26-
default = "320s"
27-
}
28-
variable "scheduler_enabled" {
29-
default = 1
30-
}
31-
variable "scheduler_request_body" {
32-
default = "{}"
33-
}
34-
variable "scheduler_request_method" {
35-
default = "POST"
36-
}
37-
variable "scheduler_retry_count" {
38-
default = 1
39-
}
40-
variable "scheduler_schedule" {
41-
description = "See https://cloud.google.com/scheduler/docs/configuring/cron-job-schedules"
42-
}
4330
variable "scheduler_service_account_email" {}
31+
variable "schedulers" {
32+
default = []
33+
type = list(object({
34+
attempt_deadline = optional(string)
35+
name = string
36+
schedule = string
37+
request_body = optional(string)
38+
request_method = optional(string)
39+
retry_count = optional(number)
40+
}))
41+
}
4442
variable "source_code_bucket_name" {}
4543
variable "source_code_root_path" {}

0 commit comments

Comments
 (0)