Publish Pub/Sub messages in a different target Google Cloud Platform (GCP) project
To change the target Google Cloud Platform (GCP) project for publishing messages from a registry to a different GCP project, follow these steps:
Identify your original project (e.g.,
skysharma-gcp
), your target project you want to publish (e.g.,skysharma-gcp-2
), your Pub/Sub topic (e.g.,root
), and subscription (e.g.,root-sub
).Give your original service account permissions in the target project as per Cloud Pub/Sub Documentation.
Update the subscription policy:
Retrieve the current policy (change yours accordingly):
CODEgcloud pubsub subscriptions get-iam-policy projects/skysharma-gcp-2/subscriptions/root-sub --format json > ~/Temp/subscription_policy.json
Edit
subscription_policy.json
to add your service account with the roleroles/pubsub.editor
. This gives the original service account access to this subscription in the new project. The updatedsubscription_policy.json
would look like this:- CODE
{ "etag": "BwUjMhCsNvY=", "bindings": [ { "role": "roles/pubsub.admin", "members": [ "user:user-1@gmail.com" ] }, { "role": "roles/pubsub.editor", "members": [ "serviceAccount:skysharma-gcp-svc-acct@skysharma-gcp.iam.gserviceaccount.com" } ] }
Apply the updated policy (change yours accordingly):
CODEgcloud pubsub subscriptions set-iam-policy projects/skysharma-gcp-2/subscriptions/root-sub ~/Temp/subscription_policy.json
Update the topic policy:
Retrieve the current policy (change yours accordingly):
CODEgcloud pubsub topics get-iam-policy projects/skysharma-gcp-2/topics/root --format json > ~/Temp/topic_policy.json
Edit
topic_policy.json
similar to the subscription policy step to add the bindings item to allow the current service account access to this topic. The updatedtopic_policy.json
would look like this:- CODE
{ "etag": "BwUjMhCsNvY=", "bindings": [ { "role": "roles/pubsub.editor", "members": [ "user:user-1@gmail.com", "serviceAccount:skysharma-gcp-svc-acct@skysharma-gcp.iam.gserviceaccount.com" ] } ] }
Apply the updated policy (change yours accordingly):
CODEgcloud pubsub topics set-iam-policy projects/skysharma-gcp-2/topics/root ~/Temp/topic_policy.json
Have the API keys for your registry ready.
Use the
curl
command to send a POST request to thechangePubSubProject
endpoint. This API changes the target project for your Pub/Sub topics.CODEcurl 'https://us-central1.clearblade.com/api/v/1/code/{SystemKey}/changePubSubProject' \ -X POST \ -d '{"project": "skysharma-gcp-2"}' \ -H 'ClearBlade-UserToken: {Token}'
Replace
{SystemKey}
and{Token}
with your specific values.If your URL is different (e.g.,
europe-west1.clearblade.com
), adjust accordingly.
Send a test MQTT message from your device into the registry. Verify the message appears in the target project’s Pub/Sub subscription.
Additional notes
The capability to change the target project is available as a new API endpoint using your registry API key.
Currently, this process has no UI; it's only available via API.
To validate, send a test message and check if it appears in the target project.