Search code examples
google-cloud-platformgcloud

How can I run a gcloud SDK command to invalidate a Cloud CDN cache remotely from my backend server?


I need to run a specific gcloud SDK command. And I need to do it remotely on my express server. Is this possible?

The command is related to the Cloud CDN service, which doesn't seem to have an npm package to access its API in an easy way. I've noticed on a cloudbuild.yaml that you can actually run a gcloud command on a build process, like:

cloudbuild.yaml

- name: "gcr.io/google.com/cloudsdktool/cloud-sdk"
  entrypoint: gcloud
  args:
    - "run"
    - "deploy"
    - "server"

And then I thought, if it's possible to run a gcloud command through Cloud Build, isn't there some way to create basically a "Cloud Script" where I could access and trigger a gcloud command, like that?


This is my environment and what I'd like to run:

  • Express server hosted on Cloud Run

I would like to run a command to clear the Cloud CDN cache, like this:

gcloud compute url-maps invalidate-cdn-cache URL_MAP_NAME \
    --path "/images/*"

There doesn't seem to be a Node.js client API to access the Cloud CDN service.


Solution

  • Here you have a REST POST endpoint https://cloud.google.com/compute/docs/reference/rest/v1/urlMaps/invalidateCache

    You can pretty much create a cloud function or call it from other places to invalidate your Cache.

    With the gcloud command you would probably have to create VM on Compute Engine and create some endpoint which execute the gcloud command no other way but I suggest you use REST endpoint as you can call it from whatever environment you use.