112 lines
3.2 KiB
Markdown
112 lines
3.2 KiB
Markdown
# Pub/Sub
|
|
|
|
A topic, a push subscription that calls a private Cloud Run service, and a dead-letter topic.
|
|
|
|
- A push subscription authenticates with `push_config.oidc_token`; its service account needs `roles/run.invoker` on the service it calls.
|
|
- Dead-lettering needs two grants to the Pub/Sub service agent: `roles/pubsub.publisher` on the dead-letter topic and `roles/pubsub.subscriber` on the source subscription. Without them messages are never moved.
|
|
- Give the dead-letter topic a subscription of its own, or the messages moved there are lost.
|
|
- `max_delivery_attempts` is 5 to 100. `ack_deadline_seconds` is 10 to 600.
|
|
- `message_retention_duration` is a duration in seconds, such as `"604800s"`.
|
|
- Customer-managed encryption is `kms_key_name` on the topic; the Pub/Sub service agent needs `roles/cloudkms.cryptoKeyEncrypterDecrypter` on the key first.
|
|
|
|
```hcl
|
|
variable "project_id" {
|
|
type = string
|
|
description = "Project to deploy into."
|
|
}
|
|
|
|
variable "region" {
|
|
type = string
|
|
description = "Region for regional resources."
|
|
}
|
|
|
|
variable "worker_url" {
|
|
type = string
|
|
description = "HTTPS URL of the service the subscription pushes to."
|
|
}
|
|
|
|
data "google_project" "this" {
|
|
project_id = var.project_id
|
|
}
|
|
|
|
locals {
|
|
pubsub_agent = "serviceAccount:service-${data.google_project.this.number}@gcp-sa-pubsub.iam.gserviceaccount.com"
|
|
}
|
|
|
|
resource "google_service_account" "push" {
|
|
project = var.project_id
|
|
account_id = "events-push"
|
|
display_name = "Identity Pub/Sub pushes with"
|
|
}
|
|
|
|
resource "google_pubsub_topic" "events" {
|
|
project = var.project_id
|
|
name = "events"
|
|
message_retention_duration = "604800s"
|
|
|
|
message_storage_policy {
|
|
allowed_persistence_regions = [var.region]
|
|
}
|
|
}
|
|
|
|
resource "google_pubsub_topic" "dead_letter" {
|
|
project = var.project_id
|
|
name = "events-dead-letter"
|
|
}
|
|
|
|
resource "google_pubsub_subscription" "dead_letter" {
|
|
project = var.project_id
|
|
name = "events-dead-letter"
|
|
topic = google_pubsub_topic.dead_letter.id
|
|
message_retention_duration = "1209600s"
|
|
|
|
expiration_policy {
|
|
ttl = ""
|
|
}
|
|
}
|
|
|
|
resource "google_pubsub_subscription" "worker" {
|
|
project = var.project_id
|
|
name = "events-worker"
|
|
topic = google_pubsub_topic.events.id
|
|
ack_deadline_seconds = 60
|
|
|
|
push_config {
|
|
push_endpoint = var.worker_url
|
|
|
|
oidc_token {
|
|
service_account_email = google_service_account.push.email
|
|
audience = var.worker_url
|
|
}
|
|
}
|
|
|
|
retry_policy {
|
|
minimum_backoff = "10s"
|
|
maximum_backoff = "600s"
|
|
}
|
|
|
|
dead_letter_policy {
|
|
dead_letter_topic = google_pubsub_topic.dead_letter.id
|
|
max_delivery_attempts = 5
|
|
}
|
|
|
|
expiration_policy {
|
|
ttl = ""
|
|
}
|
|
}
|
|
|
|
resource "google_pubsub_topic_iam_member" "agent_publishes_dead_letters" {
|
|
project = var.project_id
|
|
topic = google_pubsub_topic.dead_letter.name
|
|
role = "roles/pubsub.publisher"
|
|
member = local.pubsub_agent
|
|
}
|
|
|
|
resource "google_pubsub_subscription_iam_member" "agent_forwards_from_worker" {
|
|
project = var.project_id
|
|
subscription = google_pubsub_subscription.worker.name
|
|
role = "roles/pubsub.subscriber"
|
|
member = local.pubsub_agent
|
|
}
|
|
```
|