Using custom domain to Google App Engine with custom routing

Adding custom domain to Google App Engine is pretty straight-forward and easy to implement.

But there are some complexities around it when you want to point your custom domain or sub-domain to any specific service or module in Google App Engine. In your Google Cloud project you can have multiple App Engine service. By default your first app engine app will be considered as “default” service. Rest of the apps must need to be on another service or module. So the main use case scenario is, suppose you have a sub-domain “office-api.yourdomain.com” and you want to point that in your App Engine’s “office-api” service.

Then you can’t do that because if you point your “yourdomain.com” to Google App Engine then you can’t use that domain for your other purposes. Google will use that domain to point all services, default, custom, etc..

But if you want to use your primary business domain inside App Engine like your corporatebusiness.com domain then what you will do. You have hosted your website in this domain in another server, you have another services hosted on that domain. But for better personalized sub-domain you want to use that same doamain’s sub-domain in Google App Engine.

Here is the steps about how you can configure your sub-domain directly pointed to any specific service.

First,
You need to add your custom domain to Google App Engine from App Engine -> Settings -> Custom Domain.

app-engine-domains-previewtechs-gc-us-central-1a

Now you need to add your custom domain and need to verify your domain name. Google has a very easy way with instruction to do that. Now from your domain registrar, you need to just change your CNAME pointed to ghs.googlehosted.com domain. Only CNAME, cause you are using this domain for your other services. Only this specific sub-domain will be used in Google App Engine.

After doing that you are done! Now google has it’s own routing policy to route your application based on service-id.yourcustomsub-domain.com but you want to customized that in your way. And you can do that by appcfg.py command line python tools. You can learn that directly from¬†https://cloud.google.com/appengine/docs/python/how-requests-are-routed#routing_via_url¬†. Now in your dispatch file write the following configuration.

dispatch:
  - url: "office-api.businessdomain.com/*"
    module: office-api

And upload this dispatch file with the appcfg.py command line utilities. Documentations are available https://cloud.google.com/appengine/docs/python/config/dispatchref. Now you have your official homepage hosted on www.businessdomain.com and office-api.businessdomain.com is pointed to Google App Engine custom services. Now browse your office-api.businessdomain.com url and you will see your application. Thanks.