如何使用terraform制作具有消息排序的PubSub触发的云函数



我正在尝试创建一个由PubSub订阅触发的云函数,但我需要启用消息排序。我知道在创建链接到订阅的函数时,在google_cloudfunctions_function块中使用event_trigger块。然而,这与PubSub下描述的enable_message_ordering不同。当使用订阅推送配置时,我不知道如何将端点链接到函数。

那么,有没有一种方法可以将该功能链接到启用消息排序的订阅?我可以将函数的内部URL用作推送配置URL吗?

您不能使用PubSub和消息排序(或过滤(触发的后台函数。

您必须部署HTTP函数(注意,函数的签名会发生变化,PubSub消息的格式也会略有变化(。

然后创建一个PubSub PUSH订阅,使用Cloud Functions URL。最好的方法是在PubSub上添加一个服务帐户,只允许它调用您的函数。

为了完整起见,我想添加我用来做这件事的地形。以防其他人在看。

# 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
}

最新更新