0
votes

I'm pretty new to GCP. I need to trigger a Cloud Function(CF) when a message is published to pub/sub topic. I can do that easily when both pub/sub and CF are in the same project. But in my case, the pub/sub topic and CF are in two different projects. Can anyone suggest me how to resolve this issue? Thanks!

2
Write a pubsub function in project A that proxies the message payload to a topic in project B, and write a separate function to handle it there. You can't simply write functions that respond to topics in other projects.Doug Stevenson

2 Answers

0
votes

What Doug said in their comment.

You want to go Pub/Sub in Project A to GCF in Project B means you have a GCF in Project A that writes to Project B's Pub/Sub (you'll need to get a service account for Project B in the requirements for Project A's GCF). Then your GCF in Project B can pick up its own Pub/Sub that had the message forwarded to it from the GCF in Project A.

Adds a little bit of latency of course since it's multiple hops, but as long as the projects are in the same region, it should be minimal.

0
votes

I agree with the "proxy" solution of my 2 peers (Gabe and Doug). However, there is a lack of explanation, and I have another solution!

The problem is: you can't create a Cloud Function Topic triggered from a topic in another project. The topic MUST be in the same project as the Cloud Function (that's why a Cloud Function proxy is required).

But, you can also deploy your Cloud Function in HTTP triggered and then create a PubSub HTTP Push subscription to the Cloud Function in any project.

You can deploy your function in private mode (--no-allow-unauthenticated mode) and grand the PubSub subscription service account the right to call the Cloud Function to increase the security.