|
| 1 | +# Trigger function from Google Cloud Pub/Sub events |
| 2 | + |
| 3 | +Trigger OpenFaaS functions from Google Cloud Pub/Sub messages. |
| 4 | + |
| 5 | +> Note: This feature is included for [OpenFaaS Standard & For Enterprises](https://openfaas.com/pricing/) |
| 6 | +
|
| 7 | +## Installation |
| 8 | + |
| 9 | +* Setup the connector |
| 10 | + |
| 11 | + You can install the Google Cloud Pub/Sub connector using its [helm chart](https://github.com/openfaas/faas-netes/tree/master/chart/gcp-pubsub-connector). |
| 12 | + |
| 13 | + The `values.yaml` file can be customised to suit your needs. |
| 14 | + |
| 15 | +* Configure the connector for your needs by defining a values.yaml file |
| 16 | + |
| 17 | + ```yaml |
| 18 | + # Google cloud project ID |
| 19 | + projectID: "openfaas-381517" |
| 20 | + |
| 21 | + # List if Pub/Sub subscriptions the connector should subscribe to. |
| 22 | + subscriptions: |
| 23 | + - my-sub |
| 24 | + ``` |
| 25 | +
|
| 26 | +Use the `subscriptions` parameter to configure a list of Pub/Sub subscriptions to which the connector should subscribe. When the subscriber receives a message, the connector will attempt to invoke any function that has the subscription name listed in its `topic` annotation. |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +To test the connector, you can deploy the [printer function](https://github.com/openfaas/store-functions/blob/master/printer/handler.go). It prints out the HTTP headers and body of any invocation. |
| 31 | + |
| 32 | +Add an annotation `topic=sub1` so that the function is invoked for any message delivered to the `sub1` Pub/Sub subscription. |
| 33 | + |
| 34 | +Each time a function is invoked by the connector it will receive the message body as the HTTP body. Message attributes are mapped to HTTP Headers. |
| 35 | + |
| 36 | +```bash |
| 37 | +faas-cli store deploy printer \ |
| 38 | + --annotation topic=sub1 |
| 39 | +``` |
| 40 | + |
| 41 | +Use the [Google Cloud Console](https://console.cloud.google.com) or [gcloud CLI](https://cloud.google.com/pubsub/docs/publish-receive-messages-gcloud) to publish a message for testing. |
| 42 | + |
| 43 | +```bash |
| 44 | +faas-cli logs printer |
| 45 | +
|
| 46 | +2025-04-02T11:40:50Z X-Pubsub-Attr-Foo=[Y] |
| 47 | +2025-04-02T11:40:50Z X-Pubsub-Msg-Id=[14373415254515458] |
| 48 | +2025-04-02T11:40:50Z User-Agent=[openfaas-gateway/0.4.39] |
| 49 | +2025-04-02T11:40:50Z Accept-Encoding=[gzip] |
| 50 | +2025-04-02T11:40:50Z X-Forwarded-For=[127.0.0.1:49964] |
| 51 | +2025-04-02T11:40:50Z X-Topic=[my-sub] |
| 52 | +2025-04-02T11:40:50Z X-Forwarded-Host=[127.0.0.1:8080] |
| 53 | +2025-04-02T11:40:50Z X-Pubsub-Publish-Time=[2025-04-02T11:40:49Z] |
| 54 | +2025-04-02T11:40:50Z X-Start-Time=[1743594050512199246] |
| 55 | +2025-04-02T11:40:50Z Content-Type=[text/plain] |
| 56 | +2025-04-02T11:40:50Z X-Call-Id=[f7fccd26-d8ee-4874-90e3-eefc94e2b74c] |
| 57 | +2025-04-02T11:40:50Z X-Connector=[connector-sdk openfaasltd/gcp-pubsub-connector] |
| 58 | +2025-04-02T11:40:50Z |
| 59 | +2025-04-02T11:40:50Z Hello OpenFaaS |
| 60 | +``` |
| 61 | + |
| 62 | +Additional headers are made available to the request. These headers contain any message attributes along with some metadata about the Pub/Sub message. |
| 63 | + |
| 64 | +* `X-PubSub-Msg-ID` - the Pub/Sub message identifier. |
| 65 | +* `X-PubSub-Publish-Time` - the time at which the message was published. |
| 66 | +* `X-PubSub-Delivery-Attempt` - the number of times a message has been delivered. |
| 67 | +* `X-PubSub-Ordering-Key` - identifies related messages for which publish order should be respected. |
| 68 | +* `X-PubSub-Attr-<key>` - value of Pub/Sub message attribute. |
| 69 | + |
| 70 | +So for example, if you added an attributed called `Foo` with a value of `Y`, you'd get the following extra header: `X-PubSub-Attr-Foo: Y` |
| 71 | + |
| 72 | +The default content-type is configured as `text/plain`, but can be changed to another content-type such as `application/json` or `application/octet-stream` by the [values.yaml file](https://github.com/openfaas/faas-netes/blob/master/chart/gcp-pubsub-connector/values.yaml) for the connector. |
| 73 | + |
| 74 | +## Would you like a demo? |
| 75 | + |
| 76 | +Feel free to reach out to us for a demo or to ask any questions you may have. |
| 77 | + |
| 78 | +* [Talk to us](https://openfaas.com/pricing/) |
0 commit comments