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 から Workflows を呼び出す Terraform コード

Posted at

Terraform で Cloud Scheduler から workflows を呼び出すコードを書いてたらハマったので備忘録のついでに記事にしました。

完成形のコード

resource "google_cloud_scheduler_job" "sample" {
  name        = "sample"
  description = "サンプル"
  schedule  = "0 * * * *"
  time_zone = "Asia/Tokyo"
  project   = var.project_id
  region    = var.region
  http_target {
    uri = "https://workflowexecutions.googleapis.com/v1/projects/${var.project_id}/locations/${var.region}/workflows/${google_workflows_workflow.sample.name}/executions"
    oauth_token {
      scope                 = "https://www.googleapis.com/auth/cloud-platform"
      service_account_email = google_service_account.trigger_scraping_schedule.email
    }
    http_method = "POST"
  }
}

# IAM
resource "google_service_account" "scheduler" {
  account_id  = "scheduler"
  description = "スケジューラ用サービスアカウント"
}
resource "google_project_iam_member" "scheduler" {
  for_each = toset([
    "roles/workflows.invoker"
  ])
  role    = each.value
  member  = "serviceAccount:${google_service_account.scheduler.email}"
  project = var.project_id
}

resource "google_workflows_workflow" "sample" {
  name            = "sample"
  region          = var.region
  description     = "sample"
  service_account = google_service_account.sample_workflows.email
  call_log_level  = "LOG_ERRORS_ONLY"
  source_contents = file("your_workflow_path.yaml")
  user_env_vars = {
    PROJECT_ID = var.project_id
    REGION     = var.region
  }
}

ハマったポイント

本来 oauth_token でアクセスすべきところを oidc_token でアクセスしようとしてたためエラーが出ました。
Cloud Run Functions に対して http トリガーで実行してるのをコピペしたためうっかりしてしまいまし

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?