Reputation: 433
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
Upvotes: 17
Views: 25365
Reputation: 886
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"
}));
}
);
};
Upvotes: 13
Reputation: 2319
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.
Upvotes: 5