Jean-Baptiste
Jean-Baptiste

Reputation: 397

Is it possible to deploy a background Function "myBgFunctionInProjectB" in "project-b" and triggered by my topic "my-topic-project-a" from "project-a"

It's possible to create a topic "my-topic-project-a" in project "project-a" so that it can be publicly visible (this is done by setting the role "pub/sub subscriber" to "allUsers" on it). Then from project "project-b" I can create a subscription to "my-topic-project-a" and read the events from "my-topic-project-a". This is done using the following gcloud commands: (these commands are executed on project "project-b")

gcloud pubsub subscriptions create subscription-to-my-topic-project-a --topic projects/project-a/topics/my-topic-project-a
gcloud pubsub subscriptions pull subscription-to-my-topic-project-a --auto-ack

So ok this is possible when creating a subscription in "project-b" linked to "my-topic-project-a" in "project-a".

In my use case I would like to be able to deploy a background function "myBgFunctionInProjectB" in "project-b" and triggered by my topic "my-topic-project-a" from "project-a"

But ... this doesn't seem to be possible since gcloud CLI is not happy when you provide the full topic name while deploying the cloud function:

gcloud beta functions deploy myBgFunctionInProjectB --runtime nodejs8 --trigger-topic projects/project-a/topics/my-topic-project-a --trigger-event google.pubsub.topic.publish

ERROR: (gcloud.beta.functions.deploy) argument --trigger-topic: Invalid value 'projects/project-a/topics/my-topic-project-a': Topic must contain only Latin letters (lower- or upper-case), digits and the characters - + . _ ~ %. It must start with a letter and be from 3 to 255 characters long.

is there a way to achieve that or this is actually not possible?

Thanks

Upvotes: 3

Views: 612

Answers (1)

Mangu
Mangu

Reputation: 3325

So, it seems that is not actually possible to do this. I have found it by checking it in 2 different ways:

  • If you try to create a function through the API explorer, you will need to fill the location where you want to run this, for example, projects/PROJECT_FOR_FUNCTION/locations/PREFERRED-LOCATION, and then, provide a request body, like this one:

{

"eventTrigger": {

"resource": "projects/PROJECT_FOR_TOPIC/topics/YOUR_TOPIC",

"eventType": "google.pubsub.topic.publish"

},

"name": "projects/PROJECT_FOR_FUNCTION/locations/PREFERRED-LOCATION/functions/NAME_FOR_FUNTION

}

This will result in a 400 error code, with a message saying:

{

"field": "event_trigger.resource",

"description": "Topic must be in the same project as function."

}

It will also say that you missed the source code, but, nonetheless, the API already shows that this is not possible.

  • There is an already open issue in the Public Issue Tracker for this very same issue. Bear in mind that there is no ETA for it.

I also tried to do this from gcloud, as you tried. I obviously had the same result. I then tried to remove the projects/project-a/topics/ bit from my command, but this creates a new topic in the same project that you create the function, so, it's not what you want.

Upvotes: 1

Related Questions