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
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.
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.
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.
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.
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" }));
});
};
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With