How to create PubSub subscriptions across two different GCP projects
I’m recently working on a PubSub subscription with a Cloud Function to process events generated by a PubSub topic in a different GCP project. It isn’t straightforward on how to set it up in the GCP console as by default you can only see the topics within your own GCP project. After some digging and experimenting with my colleague, we’ve finally made it working. Here’s how.
Things we need
Project A (Topic)
- A GCP PubSub topic
test_pubsub_topic
.
Project B (Subscription)
- A GCP Cloud Function
test_print_function
that uses a HTTP trigger. Since the topic is in another GCP project, we can’t use the usual PubSub trigger. - A service account that has the permission to see the topic
test_pubsub_topic
in project A and consume the events. This account should also include “Cloud Functions Invoker” role that can invoke the cloud function.
How to set it up?
Create the PubSub topic in project A
This step is straightforward. Ask the engineer working on project A to create it. If you are responsible for that too, you can follow the documentation to create test_pubsub_topic
or in the PubSub console, click the “CREATE TOPIC” button. The rest is self-explanatory.