David Dumon
David Dumon

Reputation: 11

Google cloud function on top of google CDN

I would like to use google cloud CDN to host images for my clients. I can not discover and upload all images beforehand, and my clients may request images that are not yet stored on the CDN.

How can I add code to google cloud CDN so that if an asset does not exist yet, I can generate it before sending the HTTP response and thus not returning a 404 ?

My best bet was with google cloud functions, but that seems to be not possible (I don't see triggers linked to google cloud CDN).

Upvotes: 1

Views: 1183

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

I don't think there is any such integration between Google Cloud Functions and Google Cloud CDN.

There is however an integration between Cloud Functions and Firebase Hosting, precisely for your type of use-case. Since Firebase Hosting also uses a CDN of edge caches, the result would be the same.

The flow would roughly be:

  1. Client requests a URL.
  2. Request reaches their nearest CDN edge.
  3. The edge doesn't have data for the URL, so requests it from the origin server.
  4. On the origin server, your Cloud Functions code generates a response with cache headers on how long that response is valid.
  5. The response is stored on the CDN edge.
  6. The response is sent to the client from the CDN edge.

And any future calls for the same URL on the same edge will now skip steps 3-5.

For more on this, see the Firebase Hosting documentation on connecting to Cloud Functions.

Upvotes: 2

Related Questions