Files
gcp_solution_architecture_a…/skills/google-cloud-iac-patterns/references/cloud-run-job.md
Jonathan Boniface 5bb22d984e
Some checks failed
validation / verify (push) Failing after 8s
fixed multile issues
2026-10-07 17:33:36 +01:00

96 lines
2.7 KiB
Markdown

# Cloud Run job
`google_cloud_run_v2_job`: a task that runs to completion. It is a different resource type from a service, and its template is nested one level deeper.
What a job does not have. A Check that asks for one of these on a job cannot be met:
- `startup_cpu_boost` and `cpu_idle`. A job's `resources` block has `limits` only.
- `ingress`, `traffic`, `scaling`, `min_instance_count`, `max_instance_request_concurrency`. A job answers no requests.
- `liveness_probe`. A job's container has a `startup_probe` only.
Where a job's settings sit:
- `task_count` and `parallelism` are in `template`.
- `service_account`, `timeout`, `max_retries`, `execution_environment`, `vpc_access` and `encryption_key` are in `template.template`.
- `image`, `env`, `args`, `command` and `resources` are in `template.template.containers`.
- Whoever starts the job needs `roles/run.invoker` on it; Cloud Scheduler starts it with an HTTP call to the `:run` method.
```hcl
variable "project_id" {
type = string
description = "Project to deploy into."
}
variable "region" {
type = string
description = "Region for regional resources."
}
variable "image" {
type = string
description = "Container image reference, including the tag or digest."
}
variable "deletion_protection" {
type = bool
description = "Whether Terraform is prevented from deleting the job."
default = true
}
resource "google_service_account" "replay" {
project = var.project_id
account_id = "replay-job"
display_name = "Replay job"
}
resource "google_cloud_run_v2_job" "replay" {
project = var.project_id
name = "replay"
location = var.region
deletion_protection = var.deletion_protection
template {
task_count = 1
parallelism = 1
template {
service_account = google_service_account.replay.email
timeout = "3600s"
max_retries = 3
execution_environment = "EXECUTION_ENVIRONMENT_GEN2"
containers {
image = var.image
args = ["--mode=replay"]
resources {
limits = {
cpu = "1"
memory = "1Gi"
}
}
env {
name = "BATCH_SIZE"
value = "100"
}
}
}
}
}
resource "google_service_account" "operator" {
project = var.project_id
account_id = "replay-operator"
display_name = "Starts the replay job"
}
resource "google_cloud_run_v2_job_iam_member" "operator_runs_replay" {
project = var.project_id
location = google_cloud_run_v2_job.replay.location
name = google_cloud_run_v2_job.replay.name
role = "roles/run.invoker"
member = google_service_account.operator.member
}
```