Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use Google Cloud Pub/Sub across different Google Cloud projects?

Let's say I have project A, B and C in Google Cloud with several cloud functions in each one.

I want to setup a Pub/Sub in project C so the functions in A and B can subscribe to.

Is this possible? Do I need to setup some kind of service account with custom permissions?

Thanks

like image 223
martosoler Avatar asked Jun 29 '18 03:06

martosoler


People also ask

What are the capabilities of the cloud pub sub?

Google Cloud Pub/Sub provides messaging between applications. Cloud Pub/Sub is designed to provide reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a "topic" and other applications can subscribe to that topic to receive the messages.

What is the core difference between cloud pub/sub and cloud tasks?

In this way, Pub/Sub supports implicit invocation: a publisher implicitly causes the subscribers to execute by publishing an event. By contrast, Cloud Tasks is aimed at explicit invocation where the publisher retains full control of execution.

How do I link my Pub/Sub to Google?

Go to the Pub/Sub section of the Google Cloud console. Follow the prompt to enable the API. Click Create a topic. Publishing applications send messages to topics.

Is Google Pub/Sub same as Kafka?

While Pub/Sub Lite is conceptually similar to Apache Kafka, it is a different system with APIs more focused on data ingestion. While the differences should be immaterial for stream ingestion and processing, there are a number of specific use cases where they are important.


Video Answer


2 Answers

In the subscribing project on the IAM page, add a new member with the Pub/Sub Publisher role, the new member name is the serviceaccount-email of from the publishing project. Then create a cloud function in the publishing project and assign the same service-account in the bottom of the page(under more) to the function.

Here's a node example for a cloud function:

const PubSub = require('@google-cloud/pubsub');
const pubsub = new PubSub();
const topic = pubsub.topic('projects/subscribing-project-name/topics/topic-to- 
publish-to');
const publisher = topic.publisher();

exports.helloWorld = (req, res) => {
const customAttributes = {
message: 'Hello'
};
publisher.publish(Buffer.from("Hello from another project"), customAttributes, 
(err) => {
if (err) {
  res.status(500).send(JSON.stringify({ success: false, error: err.message }));
  return;
}
res.status(200).send(JSON.stringify({ success: true, message: "Message sent to 
pubsub topic" }));
});
};
like image 133
Calle Engene Avatar answered Sep 21 '22 07:09

Calle Engene


As you mentioned in the comment, you can subscribe to the topic from another Cloud Functions if you set a push subscription.

To publish to a Pub/Sub topic from another project you should grant the right permissions for it's service account in the destination project.

like image 20
komarkovich Avatar answered Sep 24 '22 07:09

komarkovich