Use custom domain for Google Cloud Function

2020-01-30 12:18发布

问题:

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.

回答1:

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



回答2:

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