Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom domain for Google Cloud Function

I can't see any option anywhere to set up a custom domain for my Google Cloud Function when using HTTP Triggers. Seems like a fairly major omission. Is there any way to use a custom domain instead of their location-project.cloudfunctions.net domain or some workaround to the same effect?

I read an article suggesting using a CDN in front of the function with the function URL specified as the pull zone. This would work, but would introduce unnecessary cost - and in my scenario none of the content is able to be cached so using a CDN is far from ideal.

like image 385
abagshaw Avatar asked Aug 23 '17 22:08

abagshaw


People also ask

How do I use Google Cloud with my own domain?

In the Google Cloud console, go to the Custom Domains tab of the App Engine Settings page. Click Add a custom domain. If your domain is already verified, the domain appears in the Select the domain you want to use section. Select the domain from the drop-down menu and click Continue.


2 Answers

If you connect your Cloud project with Firebase, you can connect your HTTP-triggered Cloud Functions to Firebase Hosting to get vanity URLs.

like image 190
Frank van Puffelen Avatar answered Oct 07 '22 21:10

Frank van Puffelen


Using Cloudflare Workers (CDN, reverse proxy)

Why? Because it not only allows you to set up a reverse proxy over your Cloud Function but also allows you to configure things like - server-side rendering (SSR) at CDN edge locations, hydrating API response for the initial (SPA) webpage load, CSRF protection, DDoS protection, advanced caching strategies, etc.

  1. Add your domain to Cloudflare; then go to DNS settings, add a A record pointing to 192.0.2.1 with Cloudflare proxy enabled for that record (orange icon). For example:

enter image description here

  1. Create a Cloudflare Worker script similar to this:
function handleRequest(request) {   const url = new URL(request.url);    url.protocol = "https:";   url.hostname = "us-central1-example.cloudfunctions.net";   url.pathname = `/app${url.pathname}`;      return fetch(new Request(url.toString(), request)); }  addEventListener("fetch", (event) => {   event.respondWith(handleRequest(event.request)); }); 
  1. Finally, open Workers tab in the Cloudflare Dashboard, and add a new route mapping your domain URL (pattern) to this worker script, e.g. example.com/* => proxy (script)

For a complete example, refer to GraphQL API and Relay Starter Kit (see web/workers).

Also, vote for Allow me to put a Custom Domain on my Cloud Function in the GCF issue tracker.

like image 22
Konstantin Tarkus Avatar answered Oct 07 '22 23:10

Konstantin Tarkus