Ben Donnelly
Ben Donnelly

Reputation: 33

How to make a PubSub triggered Cloud Function with message ordering using terraform?

I am trying to create a Cloud Function that is triggered from a PubSub subscription, but I need to have the message ordering enabled. I know to use the event_trigger block in the google_cloudfunctions_function block, when creating a function linked to a subscription. However this does not like the enable_message_ordering as described under PubSub. When using the subscription push config, I don't know how I can get link the endpoint to the function.

So is there a way I can link the function to a subscription with message ordering enabled? Can I just use the internal URL to the function as the push config URL?

Upvotes: 1

Views: 2042

Answers (2)

Ben Donnelly
Ben Donnelly

Reputation: 33

For completeness I wanted to add the terraform that I used to do this. In case others are looking.

# This is the HTTP function that processes the events from PubSub, note it is set as a HTTP trigger
resource "google_cloudfunctions_function" "processEvent" {
  name = "processEvent"
  runtime = var.RUNTIME

  environment_variables = {
    GCP_PROJECT_ID = var.GCP_PROJECT_ID
    LOG_LEVEL = var.LOG_LEVEL
  }

  available_memory_mb = var.AVAILABLE_MEMORY
  timeout = var.TIMEOUT
  source_archive_bucket = var.SOURCE_ARCHIVE_BUCKET
  source_archive_object = google_storage_bucket_object.processor-archive.name
  trigger_http = true
  entry_point = "processEvent"
}

# define the topic
resource "google_pubsub_topic" "event-topic" {
  name = "event-topic"
}

# We need to create the subscription specifically as we need to enable message ordering
resource "google_pubsub_subscription" "processEvent_subscription" {
  name  = "processEvent_subscription"
  topic = google_pubsub_topic.event-topic.name

  ack_deadline_seconds = 20

  push_config {
    push_endpoint = "https://${var.REGION}-${var.GCP_PROJECT_ID}.cloudfunctions.net/${google_cloudfunctions_function.processEvent.name}"
    oidc_token {
      # a new IAM service account is need to allow the subscription to trigger the function
      service_account_email = "cloudfunctioninvoker@${var.GCP_PROJECT_ID}.iam.gserviceaccount.com"
    }
  }
  enable_message_ordering = true
}

Upvotes: 2

guillaume blaquiere
guillaume blaquiere

Reputation: 75940

You can't use background functions triggered by PubSub and message ordering (or filtering).

You have do deploy a HTTP functions (take care, the signature of the fonction change, and the the format of the PubSub message also change slightly).

Then create a PubSub PUSH subscriptions, use the Cloud Functions URL. The best is also to add a Service Account on PubSub to allow only it to call your Functions.

Upvotes: 1

Related Questions