Deploying a Rails API on either Heroku/AWS Elastic Beanstalk Deploying a Rails API on either Heroku/AWS Elastic Beanstalk json json

Deploying a Rails API on either Heroku/AWS Elastic Beanstalk


The problem with heroku is that if you don't pay for a domain, and just use the base service, it will assign a subdomain for your app. So all I needed to do was to redirect heroku to a custom url such as api.any_domain.com.

An easier alternative would be to override the constraints for the api on the routes.rb file:

  namespace :api, defaults: { format: :json }, path: '/api'  do

You will now be able to see the API under http://herokuapp.com/api/any_endpoint


Steps on how to configure Heroku for a Rails API

  1. First you need to buy a domain using any registrar, say you buy www.example.com, so you can use a subdomain like api.example.com. Once that's done you need to tell heroku to listen to requests at api.example.com. This can be done easily by typing this on your cmd

    heroku domains:add api.example.com
  2. After using the domains:add command you must point your DNS provider at the domain’s DNS Target supplied by Heroku. This is done by creating a CNAME entry to something like example.herokuapp.com (type heroku info to get the name of your app). The above process is made simple if you use a service like DNSimple (https://www.youtube.com/watch?v=rr6Jy9i0Cws). Basically you add a CNAME record of api.example.com which points to example.herokuapp.com

  3. One important thing everyone forgets once they deploy to heroku is to run the heroku run rake db:migrate command.

Once all this is done you would be able to access the api using api.example.com/any_endpoints

Additional resources: https://devcenter.heroku.com/articles/custom-domainshttps://support.dnsimple.com/articles/alias-record/