0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cloud Schedulerを使ってCloud Runを定期実行しよう!

Posted at

概要

Cloud Schedulerを使ってCloud Runを定期実行する方法について解説します

実装

Cloud Scheduler用のService Accountを作成します
今回はCloud SchedulerのAdmin権限とCloud Runを実行するinvokerの権限を付与します

iam.tf
resource "google_service_account" "cloud_scheduler_sa" {
  account_id   = "cloud-scheduler-jobs-invoker"
  display_name = "Cloud Scheduler Jobs Invoker"
}

resource "google_project_iam_member" "cloud_scheduler_jobs_admin" {
  project = var.project
  role    = "roles/cloudscheduler.admin"
  member  = "serviceAccount:${google_service_account.cloud_scheduler_sa.email}"
}

resource "google_project_iam_member" "cloud_scheduler_jobs_invoker" {
  project = var.project
  role    = "roles/run.invoker"
  member  = "serviceAccount:${google_service_account.cloud_scheduler_sa.email}"
}

Cloud Schedulerを作成します
今回は5分に1回実行するよう設定します
また、uriは以下のようにgoogleapis.comを使って指定しているので認証はoauth_tokenを使用します

scheduler.tf
resource "google_cloud_scheduler_job" "job_scheduler" {
  name        = "trigger-sample-job"
  region      = var.region
  description = "Trigger Cloud Run Job every 5 minutes"
  schedule    = "*/5 * * * *"
  time_zone   = "Asia/Tokyo"

  http_target {
    http_method = "POST"
    uri         = "https://${google_cloud_run_v2_job.cloud_run_job.location}-run.googleapis.com/apis/run.googleapis.com/v1/namespaces/${var.project_number}/jobs/${google_cloud_run_v2_job.cloud_run_job.name}:run"

    oauth_token {
      service_account_email = google_service_account.cloud_scheduler_sa.email
    }
  }
}

実際に実行してみよう!

以下のように5分に一回Cloud Runが定期実行できれば成功です

スクリーンショット 2025-04-06 22.55.53.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?