I’m currently a teacher assistant 👨💻@ Técnico Lisboa, and I’m working closely with Dr. Professor Rui Cruz 👨🏫at the It Infrastructure Management and Administration 🖥, a masters level course @ Técnico Lisboa.
My experience as a professor opened my mind on how to use certain tools to achieve goals that me, as a student, want to reach. As a student, I’m responsible for a student’s group, GCE. Our project had some problems: it was very difficult and time-consuming to move from the conceptualization of an idea to its actually delivery.
A good, automated infrastructure was the answer to our problem. It allowed saving lots of time and effort, which translated into a better product delivered faster. This technique is meant for the ones that want to deliver as much as possible to the client as quickly as possible.
- We have three different apps for our three different environments (development, staging, and production).
- All the apps have MongoLab installed, and a utility to manage logs (Papertrail).
- Each app is linked with a branch of our repository
This allows for extreme flexibility. The workflow can be something like:
- Developer João creates a branch dedicated to implementing a matchmaking between companies and students — branch “matchmaking”.
- João codes all night.
- João makes a pull request to the branch “develop”. A battery of tests is run when the request is made (we didn’t implement this so far).
- Inês, a product manager, checks the pull request. Tests pass and she sees everything is fine. She accepts the pull request.
- By now, Heroku is building the development application — automatically.
- The whole team can see and interact with the new version of the product, in a test environment.
- When there is enough functionality on the branch “develop”, one pull request can be made to the branch “staging” (has an environment similar to production) and after that to the branch “master” (production).
At the end of the day, you have saved lots of precious minutes, which can be very representative in one semester.
Okay, I’m convinced. Let’s get to the action!
The idea is to have a virtual machine as the machine which will configure our infrastructure on Heroku, through Terraform. We will manage this virtual machine using Vagrant.
If you want to know how Terraform works, check this tutorial (up to “Requirements”, 1min read). You can achieve the same purpose without a virtual machine. In that case, skip directly to Step 2. What we will do is well condensed in the following scheme:
Step 1: Installing Vagrant
Step 2: Start the Virtual Machine
Now, open your console and run:
$ vagrant up --provision && vagrant ssh
The virtual machine is booting, installing all the required dependencies. This may take a while.
Wait a bit. Done. Nice. Kudos to you 👍
Step 3: Getting the Support Files
Fork this repository containing the support files and then clone it to your computer. Put it in the folder that is being synced with your guest machine.
Step 4: Analyse Terraform Files
Now, replace the GitHub repositories and Heroku credentials on the file terraform-vars.tfvars.
Notice what it’s being done on terraform-apps.tf.
Step 5: Authenticate in Heroku through the Cli
To authenticate, run:
$ heroku login --interactive
Put your credentials and login. Next, we need to generate an access token to Terraform to be able to communicate with Heroku. Run:
$ heroku authorizations:create
This will return you something like this:
Go to terraform-vars.tf and paste the token obtained in the field “heroku_api_key”.
Step 6: Initialize Terraform
$ terraform init
Step 7: Deploy Your Infrastructure
To know what you are going to deploy, run:
$ terraform plan
If everything is correct, run:
$ terraform apply --auto-aprove
Congratulations, your infrastructure is on! 💯
Double check it on Heroku’s dashboard:
I recommend you to configure the deployment method on each app, by choosing a GitHub repository and associate it with a branch. For instance, we could associate the “develop” branch to the development app. When a commit occurs, it will be automatically deployed.
Destroying the infrastructure
With two commands you will end the experiment and shut down the virtual machine. These are:
$ terraform destroy -auto-approve
$ vagrant halt
If you want to destroy the virtual machine you created, run:
$ vagrant destroy --auto-approve
Congratulations! 💯 You reached the end! 🦄
This is a very good infrastructure for MVPs, although it might not be the best in the long run. Nextly, I’m going to talk about Kubernetes and how we can deliver an infrastructure similar to this, but much more scalable. Stay tuned 😀