76 lines
2.0 KiB
Markdown
76 lines
2.0 KiB
Markdown
# Cloud Scheduler
|
|
|
|
A schedule that starts a Cloud Run job, and one that calls a private Cloud Run service.
|
|
|
|
- A job is started by an HTTP POST to its `:run` method with an OAuth token (`oauth_token`). A service is called with an OIDC token (`oidc_token`). Using the wrong one is refused at run time, not by `terraform validate`.
|
|
- The schedule's service account needs `roles/run.invoker` on the job or service it starts.
|
|
- `schedule` is unix-cron; `time_zone` is an IANA name.
|
|
|
|
```hcl
|
|
variable "project_id" {
|
|
type = string
|
|
description = "Project to deploy into."
|
|
}
|
|
|
|
variable "region" {
|
|
type = string
|
|
description = "Region for regional resources."
|
|
}
|
|
|
|
variable "job_name" {
|
|
type = string
|
|
description = "Name of the Cloud Run job to start."
|
|
}
|
|
|
|
variable "service_url" {
|
|
type = string
|
|
description = "HTTPS URL of the private Cloud Run service to call."
|
|
}
|
|
|
|
resource "google_service_account" "scheduler" {
|
|
project = var.project_id
|
|
account_id = "nightly-scheduler"
|
|
display_name = "Identity of the nightly schedules"
|
|
}
|
|
|
|
resource "google_cloud_scheduler_job" "start_job" {
|
|
project = var.project_id
|
|
region = var.region
|
|
name = "start-nightly-job"
|
|
schedule = "0 2 * * *"
|
|
time_zone = "Etc/UTC"
|
|
attempt_deadline = "180s"
|
|
|
|
retry_config {
|
|
retry_count = 3
|
|
}
|
|
|
|
http_target {
|
|
http_method = "POST"
|
|
uri = "https://run.googleapis.com/v2/projects/${var.project_id}/locations/${var.region}/jobs/${var.job_name}:run"
|
|
|
|
oauth_token {
|
|
service_account_email = google_service_account.scheduler.email
|
|
}
|
|
}
|
|
}
|
|
|
|
resource "google_cloud_scheduler_job" "call_service" {
|
|
project = var.project_id
|
|
region = var.region
|
|
name = "call-nightly-service"
|
|
schedule = "30 2 * * *"
|
|
time_zone = "Etc/UTC"
|
|
|
|
http_target {
|
|
http_method = "POST"
|
|
uri = "${var.service_url}/tasks/nightly"
|
|
|
|
oidc_token {
|
|
service_account_email = google_service_account.scheduler.email
|
|
audience = var.service_url
|
|
}
|
|
}
|
|
}
|
|
```
|