Use custom domain for Google Cloud Function
Asked Answered
C

5

64

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.

Claudclauddetta answered 23/8, 2017 at 22:43 Comment(2)
Did you ever find a workaround?Serrated
For what's it worth, this recent answer claims it is not possible without Firebase #56469937Styrene
S
32

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

Scintillator answered 24/8, 2017 at 5:45 Comment(9)
Thanks - I do recall reading somewhere that Google Cloud Functions for Firebase supports this feature. Upvoted, but not going to mark as answer as hopefully some day this functionality will be supported natively or a workaround will come up.Claudclauddetta
Well, we are very close to an answer! Now there is Google Cloud Run, new product announced at GCN'19, it works the same as a google cloud function, but with containers. If you want to continue to use Node, there are very quick and easy ways to achieve the same result starting from an official docker image.Orella
With Cloud Run too the way to get a custom domain is through Firebase Hosting.Scintillator
Is it possible to run the function by just calling the domain? I have tried but looks like the function is not called if I simply access the domain www.mydomain.comSeismology
Ok I will answer my own comment. Yes it is possible, by simply removing "public": "public", from the firebase.jsonSeismology
@FrankvanPuffelen I was able to get a custom domain working on a Cloud Run service without interacting with Firebase at all. The process involved clicking on "custom domains" in the Cloud Run area of the GCP console and following the steps to add DNS records to prove domain ownership. It then automatically generated a TLS cert for my domain and set it up. I didn't need to delegate any DNS functionality to GCP to do this, I was able to set the records needed in my non-GCP DNS provider.Mariahmariam
Good to hear Matt. Cloud Run indeed seems to support a more direct custom domain mapping. Since this question was about Cloud Functions, my answer is still the way to go or that. But you could consider posting an answer about using Cloud Run instead, and how to set up a custom domain for that.Scintillator
Note, that this only works correctly using ' us-central1', see: #48875171Thickwitted
This option will cost you more $$$ #59965434Exhortative
M
20

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:
const API_URL = "https://us-central1-example.cloudfunctions.net/api";

export default {
  async fetch(req) {
    const { pathname, search } = new URL(req.url);
    return await fetch(`${API_URL}${pathname}${search}`, req);
  }
}
  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 React Starter Kit (★22k) (see /edge folder).

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

Mcclung answered 26/3, 2021 at 11:16 Comment(1)
Although I don't believe this is the best method to add a custom domain to a serverless endpoint, I have to agree that Cloudflare Workers are an amazing product. If you're looking to check out something cool while building your product, give this a try!Flycatcher
P
15

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

Proxy answered 23/11, 2020 at 12:12 Comment(1)
Works for me! Thanks)!Boony
F
9

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

Forging answered 28/10, 2019 at 12:42 Comment(6)
not sure how you got that working, my GCP project is connected with a firebase project, what's myFunction, since GCP functions can have same name and be deployed to different region. When trying with a function name, I'm getting a 302 error, I can curl the function in question with its cloudfunctions.net domain. Thoughts?Upward
would you update the answer so that it makes more sense as to where myfunction/custom comes from.Flair
@Steve, myfunction/custom is just a completely arbitrary path chosen as for the url for the cloud function - assuming "example.com/" is your domain, you can chose the path to be "myfunction/custom", "bob/and/alice", "this/is/a/silly/example", etc. OP happened to use the name of his cloud function "myfunction" as part of the path, but that's just arbitrary - they don't have to have the same name.Bagel
@Bagel so in reality it wouldn't be a relative url, it would be some generic cloud function url like https://{region}-{project}/{function}Flair
Quite exactly. You can "name" the user-visible domain and path arbitrarily, just as you can "name" your function (but not the function domain) arbitrarilyBagel
It works, I have just tested it! However, the documentation warns it is only possible for Cloud Functions placed in "us-central1" region. See firebase.google.com/docs/hosting/…Sapers
E
-6

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

Enfranchise answered 23/5, 2020 at 7:55 Comment(2)
Are u sure this CNAME works? As far as I know, it is not allowed by googleNarration
tested and 100% doesnt work: will return 404 like func.yaskur.com/examplePretence

© 2022 - 2024 — McMap. All rights reserved.