In the previous article, I built a microservice project that scrapes motivational quotes from the Internet. Now, I’m exploring how could I deploy this project for free.
After some research, I’ve found that there are a lot of free resources out there. I decided to get started with Heroku as it seems like a good fit for my project.
- Free Tier
- Free CloudAmqp Service (RabbitMQ as a Service)
This hands-on section features several parts:
- Project set up in Heroku
- RabbitMQ set up in Heroku
- Integrating RabbitMQ with your projects
- Problems that I encountered
Project set up in Heroku
- Set up Heroku on Mac.
brew tap heroku/brew && brew install heroku
2. Create an app in the Heroku Dashboard.
3. Login to Heroku. Insert your credentials and you should be able to login.
heroku: Press any key to open up the browser to login or q to exit:
4. I have my Node project ready. Thus, I only need to add my existing project to Heroku Remote. Here are the steps required.
If you haven’t committed your code into git, you might need to perform the following commands.
// Add all your existing file
git add .// Commit all your changed file
git commit -am "Added feature ???"// Push to Heroku Master
git push heroku master
If you already committed all your codes, you just have to push it to Heroku origin, master branch.
// Push to Heroku Master
git push heroku master
As long as you push to Heroku master branch, Heroku will help you build and run the Node project. You’ll see your build log on your dashboard, and it will show whether the build succeeded or failed, followed by whether it was successfully deployed.
RabbitMQ set up in Heroku
Now, let’s start the RabbitMQ service on Heroku.
- Go to your Heroku Dashboard.
- Pick one of the microservices that you deployed. For me, I picked the TimerService that I built.
- You’ll see a dashboard that looks like the one in the screenshot below. I’ll provide several screenshots to guide you on how to set up RabbitMQ Add-ons. I believed screenshots illustrate how to do this better than my words here.
After clicking on the installation, you’ll see the different types of plans available for RabbitMQ. Select an app that you want to host the CloudAMQP. Lastly, click on Provision add-on, as shown in the screenshot below.
Now you have a working RabbitMQ service.
Integrate Heroku RabbitMQ with the project
Now we have a working project deployed to Heroku and a working RabbitMQ service. What we’re missing is integration. We have to configure our projects to publish messages and listen to messages from the defined RabbitMQ service.
Integrating was easy. We just have to update the
process.env.CLOUDAMQP_URL , as shown in the gist above.
In order to get the RabbitMQ URL, you just have to click on the CloudAMQP add-ons from your Heroku app dashboard. Below is a screenshot of what you’ll see after you click on the CloudAMQP add-ons.
Just copy the AMQP URL and paste it in the
.env file. Then, redeploy it again to your application on Heroku, and it will be working.
Below is the screenshot of my RabbitMQ Manager when my microservices connected to it.
Problems that I encountered
During the whole deployment learning process, I encountered two problems.
- You must declare your start scripts in
package.json. In order to start your project, you need to do
node server.js. Then you have to declare it in your
package.jsonscripts. This is because Heroku will start your project by performing
- The second problem is if I have 10 microservices, do I have to declare 10 RabbitMQ add-ons for each node app? This somehow defeats the purpose, right? It turns out it doesn’t. I just try to use the same RabbitMQ service across all the Node projects — it turns out it can be shared. That RabbitMQ service only requires a single app to be attached. Then, it can be shared among all microservices as long as the URL is correct.
If you encountered the same problems as me, I hope this helps you. If you encounter other problems, you can always Google them or ask for help.
In short, I felt like it was pretty easy to deploy to Heroku after you know how to use it. It feels great when you don’t have to manage the server set up and tools installation.
Thanks for reading and enjoy developing.