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 Pub/SubからCloud Runを呼び出すときに、Eventarcのタイムアウトを変更する方法

Posted at

Cloud Pub/SubからCloud RunやCloud Run Functionsを呼び出す時には、以下のように設定します。
直接Cloud Pub/SubのSubscriptionを作成するのではなく、Eventarcを使って間接的にSubscriptionを作成しています。

resource "google_pubsub_topic" "invoke-function" {
  name = "invoke-function"
}

resource "google_service_account" "invoke-function" {
  account_id   = "invoke-function"
}

resource "google_project_iam_member" "eventreceiver" {
  project = "<プロジェクトID>"
  role    = "roles/eventarc.eventReceiver"
  member  = google_service_account.invoke-function.member
}

resource "google_project_iam_member" "runinvoker" {
  project = "プロジェクトID"
  role    = "roles/run.invoker"
  member  = google_service_account.invoke-function.member
}

resource "google_eventarc_trigger" "invoke-function" {
  name     = "invoke-function"
  location = "asia-northeast1"

  matching_criteria {
    attribute = "type"
    value     = "google.cloud.pubsub.topic.v1.messagePublished"
  }

  transport {
    pubsub {
      topic = google_pubsub_topic.invoke-function.id
    }
  }

  destination {
    cloud_run_service {
      service = "Cloud Runのサービス名"
      region  = "asia-northeast1"
    }
  }

  service_account = google_service_account.invoke-function.email
}

このとき、Eventarcは以下のようなSubscriptionを裏側で作成します。

SCR-20250427-lbgt.png

Acknowledgement deadlineやMinimun/Maximum backoff durationなどはEventarcが勝手に設定するものです。

そのため、例えばCloud Runの処理時間が10sを超える場合には、ACK応答がないと判断されて自動的にリトライ処理が実行されてしまいます。この設定値を変えたい場合は、google_eventarc_triggerリソース経由で変更することはできず、直接Subscriptionをいじる必要があります。

Webコンソールからいじるのであれば、上記の画面のEDITボタンから簡単にできるのですが、terraformでこれらをいじる方法がやや技巧的だったので残しておきます。

importブロックのidは他のリソースの属性を参照できるので、Eventarcが裏で作成するSubscriptionのIDを指定しています。ack_deadline_secondsを変更するとACK応答の待ち時間を変更できます。

import {
  id = google_eventarc_trigger.invoke-function.transport[0].pubsub[0].subscription
  to = google_pubsub_subscription.invoke-function
}

resource "google_pubsub_subscription" "invoke-function" {
  name  = basename(google_eventarc_trigger.invoke-function.transport[0].pubsub[0].subscription)
  topic = google_eventarc_trigger.invoke-simple-function.transport[0].pubsub[0].topic

  ack_deadline_seconds = 10 # ここを上書きするとACK応答の待ち時間を変更できる

  # 普通にSubscriptionを作成する場合のデフォルト値とEventarcのデフォルト値が異なるので、
  # Eventarc側に合わせておく
  message_retention_duration = "86400s"

  # retry_policyはEventarcとterraformでデフォルト値が一致しているが、
  # これを指定しないとapply時に差分があるような見た目になるので念の為に指定する
  retry_policy {
    maximum_backoff = "600s"
    minimum_backoff = "10s"
  }

  # push_configはEventarcの設定から変更しないため
  lifecycle {
    ignore_changes = [
      push_config
    ]
  }
}

なお、この方法はEventarcのタイムアウト値を変更しているだけなので、必要に応じてCloud Run側のタイムアウト値も変更する必要があります。

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?