[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? Follow in this order:

  1. 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.
  2. Collect the DNS value in these steps:
    1. Go to the app’s Settings and add a new custom domain which you want to add.
    2. You may have to verify your account beforehand via credit card.  If required, then please do so.
    3. Add your custome domain.
    4. Once you add the custom domain, heroku will give you corresponding value for DNS target. Copy this DNS value, 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-name-123456.herokuapp.com)
  3. Go to cloudflare and open DNS tab of the domain (where the subdomain will be created)
  4. 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 DNS value from step-ii. I will suggest to use ii.iv with caution (just try to create the app’s name in a unique way so that no one can guess it)
  5. Done 🙂