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.
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.
If you connect your Cloud project with Firebase, you can connect your HTTP-triggered Cloud Functions to Firebase Hosting to get vanity URLs.
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.
DNS
settings, add a A
record pointing to 192.0.2.1
with Cloudflare proxy enabled for that record (orange icon). For example: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)); });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With