Google Cloud Pub/Sub
Publish Dataflow's events to a Google Cloud Pub/Sub topic, with a service account that only has the Publisher role on it.
Each event becomes one message on your topic. Dataflow signs in with a service account key whose account may only publish to that topic.
Set up the topic
Create the topic
In the console, Pub/Sub → Topics → Create topic, or:
gcloud pubsub topics create dataflow-events --project my-projectAdd a subscription to it, so the messages are kept until you read them.
Create a service account with the Publisher role on that topic
gcloud iam service-accounts create dataflow-publisher --project my-project
gcloud pubsub topics add-iam-policy-binding dataflow-events --project my-project \
--member "serviceAccount:dataflow-publisher@my-project.iam.gserviceaccount.com" \
--role roles/pubsub.publisherGrant the role on the topic, not on the project: the account can then publish to this topic and nothing else.
Create a key
gcloud iam service-accounts keys create key.json \
--iam-account dataflow-publisher@my-project.iam.gserviceaccount.comIf your organization blocks key creation (iam.disableServiceAccountKeyCreation), an administrator needs to allow it for this project.
Add the destination
The key goes in credentials.service_account_json as a string: the file's content, as Google downloads it.
{
"type": "gcp_pubsub",
"config": { "project_id": "my-project", "topic": "dataflow-events" },
"credentials": {
"service_account_json": "{\"type\": \"service_account\", \"project_id\": \"my-project\", \"private_key_id\": \"KEY_ID\", \"private_key\": \"-----BEGIN PRIVATE KEY-----\\n…\\n-----END PRIVATE KEY-----\\n\", \"client_email\": \"dataflow-publisher@my-project.iam.gserviceaccount.com\", \"token_uri\": \"https://oauth2.googleapis.com/token\"}"
},
"topics": ["*"]
}With jq, the key file goes in as it is:
jq -n --rawfile key key.json '{
type: "gcp_pubsub",
config: { project_id: "my-project", topic: "dataflow-events" },
credentials: { service_account_json: $key },
topics: ["*"]
}' | curl https://dataflow.piloterr.com/v1/destinations \
-H "x-api-key: YOUR_API_KEY" -H "content-type: application/json" --data @-| Field | Required | Secret | Holds |
|---|---|---|---|
config.project_id | yes | no | The topic's project id, such as my-project. |
config.topic | yes | no | The topic's id, dataflow-events, not its full path. |
credentials.service_account_json | yes | yes | The service account key file's JSON, as a string. It must hold type: "service_account", client_email and private_key. |
The key is stored encrypted and shown as …. To change it, send it again with PATCH. See Credentials.
Network
Messages go to the Pub/Sub API over TLS (443), from our egress address, 116.203.84.164 (exit_ips of GET /v1/destinations/types). There is nothing to open on your side.
What the topic receives
- Data: the event payload, as JSON.
- Attributes:
event-id(the payload'sid: deduplicate on it),topic(such asrun.completed) andtimestamp(when it was sent, RFC 3339).
A subscription filter can keep some topics only, for example attributes.topic = "run.completed".
Test it
Send a test event from the dashboard, or POST /v1/destinations/{id}/test (details), then pull it:
gcloud pubsub subscriptions pull YOUR_SUBSCRIPTION --project my-project --auto-ackThe delivery log's code is OK, or ERR for any failure.
| Problem | Fix |
|---|---|
422 validation_failed on credentials.service_account_json | Paste the whole key file, as a string. |
422 validation_failed on config.topic | Give the topic's id, without projects/…/topics/. |
ERR on every delivery | Check, in this order: the service account has roles/pubsub.publisher on this topic; the key is not deleted or disabled; the project id and topic id are the topic's. |
OK but no message to pull | The subscription was created after the event, or its filter leaves it out. |
Other errors: Errors.