[P04] Serve custom domain via Heroku

Heroku is one of the most popular PAAS provider, I am using this to show serve a very small yet impactful service: Redirect various product url from a single source.

Example first, I maintain a date conversion library (which we migrated to Jest). In order to share its url to all, I can give the Github url to them. Or, I can create a subdomain in my primary domain and redirect visitors to that particular Github page.
The second approach will give a bit personalized touch than the former one. So, I took this aproach and I've implemented the redirection via Python3 and Flask; and made use of heroku apps to scale it. Once it was published via Heroku, I created custom DNS direction in Cloudflare and the resultant heroku app is served via http://redirect.mythicangel.com/bangladatejs url. Being in PAAS system,  it is cloud native and scalable 🙂

So, what are the steps? I have followed these steps:

  1. Local development: Create a new python app by following official instruction from Heroku.
    Just remember 2 thing:

    1. If you're using Ubuntu, then don't follow their guideline to install cli from source. Instead download it via snap command.
    2. Must use virtualenv to avoid the installation mess of python2 and python3. Alternatively (and also recommended), you can use pipenv
  2. Destination app: Setup empty app in Heroku, let's say it's reachable with example.herokuappps.com url
  3. DNS setup for CNAME: In order to tell Heroku that we are going to setup a domain/subdomain to this particular app, we have to setup DNS entry in Heroku. For this, collect the DNS value in these steps:
    1. Go to the Heroku app's Settings and add a new custom domain which you want to add (in my case redirect.mythicangel.com)
    2. You may have to verify your account beforehand via credit card.  If required, then please do so.
    3. Add your custom domain.
    4. Once you add the custom domain, heroku will give you corresponding value for DNS target. Copy this DNS value (e.g. sample-dns-value.herokuapp.com , you'll need to use as CNAME value in your DNS registrar (We don't this value for cloudflare by the way)
    5. Just copy the app's url without https:// prefix (e.g. example.herokuapp.com)
  4. Connect the CNAME in DNS registrar: There might be 2 possibilities.
    1. If you're not using Cloudflare, then copy the DNS value from step iii-iv. Then enter your subdomain there and use CNAME record type where you'll give the DNS value
    2. If you're using Cloudflare (which you should in my opinion 🙂 ), then go to cloudflare and open DNS tab of the domain (where the subdomain will be created). Add a new record of type "CNAME" (bydefault A will be selected). In the subdomain field, give your desired subdomain (e.g. redirect as in my case) and in the CNAME field, give the app's name (example.herokuapp.com in my case)
      By the way, I will suggest to choose the app's name with caution to avoid direct access (just try to create the app's name in a unique way so that no one can guess it)
  5. Done 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *