Use custom domain for Google Cloud Function

Google Cloud-PlatformGoogle Cloud-Functions

Google Cloud-Platform Problem Overview


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.

Google Cloud-Platform Solutions


Solution 1 - Google Cloud-Platform

Solution 2 - Google Cloud-Platform

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.

Solution 3 - Google Cloud-Platform

Another way to do it while avoiding Firebase is to put a load balancer in front of the Cloud Function or Cloud Run and use a "Serverless network endpoint group" as the backend for the load balancer.

Once you have the load balancer set up just modify the DNS record of your domain to point to the load balancer and you are good to go.

https://cloud.google.com/load-balancing/docs/https/setting-up-https-serverless

Solution 4 - Google Cloud-Platform

Been a while for this answer.

Yes, now you can use a custom domain for your Google Cloud functions.

Go over to firebase and associate your project with firebase. What we are interested in here is the hosting. Install the Firebase CLI as per the firebase documentation - (very good and sweet docs here)

Now create your project and as you may have noticed on the docs, to add firebase to your project you type firebase init. Select hosting and that's it.

Once you are done, look for the firebase.json file. Then customize it like this

{

  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "myfunction/custom",
        "function": "myfunction"
      },
    ]
  }
}

By default, you get a domain like https://project-name.web.app but you can add your own domain on the console.

Now deploy your site. Since you are not interested in web hosting probably you can leave as is. Now your function will execute like this

Function to execute > myfunction

Custom url > https://example.com/myfunction/custom

Solution 5 - Google Cloud-Platform

If you don't mind the final appearance of the url you could also setup a CNAME dns record.

function.yourdomain.com -> us-central1******.cloudfunctions.net

then you could call it like

function.yourdomain.com/function-1/?message=Hello+World

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionabagshawView Question on Stackoverflow
Solution 1 - Google Cloud-PlatformFrank van PuffelenView Answer on Stackoverflow
Solution 2 - Google Cloud-PlatformKonstantin TarkusView Answer on Stackoverflow
Solution 3 - Google Cloud-Platformzo.olView Answer on Stackoverflow
Solution 4 - Google Cloud-PlatformLucemView Answer on Stackoverflow
Solution 5 - Google Cloud-PlatformtavisView Answer on Stackoverflow