Here are threenine we are a small team that takes on big projects, and because of these we have a strong focus on keeping things simple, lean and agile. Over the years, we have developed and evolved our own processes. Often, completely disregarding the text book best practices, because quite frankly they just don't work. That sentence alone requires an entire series of articles which we won't go into here, but most likely will in other articles.
With all those points said, we are always looking for ways to optimise and improve the way we work and can get things quickly and efficiently. One of these approaches we have adopted is the Infrastructure as Code methodology. Our tool of choice over the years has become Terraform, especially when working with Cloud Providers.
Terraform is a tool for building and managing infrastructure in an organized way. You can use it to manage virtual machines, containers, Load Balancers, and even DNS entries, in addition to a large variety of services offered by other providers. Terraform uses a command-line interface and can run from your desktop or a remote server.
Terraform works by reading configuration files that describe the components that make up your application environment or datacenter. Based on the configuration, it generates an execution plan that describes what it will do to reach the desired state. You then use Terraform to execute this plan to build the infrastructure. When changes to the configuration occur, Terraform can generate and execute incremental plans to update the existing infrastructure to the newly described state.
Although, AWS and Azure, seem to be the leaders in the Enterprise Cloud Native game, we have developed a strong preference to using DigitalOcean. In our experiences, for the smaller companies, start ups and bootstrappers, the so called leading platforms have have really complicated billing and unpredictable billing practices and its not always easy to work out and stick to a budget. This is where in my opinion Digital Ocean delivers its true value along with providing a great scalable and performant platform to build your new ideas on.
This post will guide you through the process of install Terraform and configuring it to work on your Linux development workstation.
Prerequisites
In order to complete the steps in this article, there are a few things you need to have in place.
- A Digital Ocean Account, if you do not have one yet, Sign up for a new Digital Ocean account and get free credit.
- We are a solely Linux-based software development house, so we recommend that you be using Linux to following along.
- We recommend using Homebrew package manager when installing developer tools it seems to be more frequently updated than the official repositories, when it comes to developer tools.
How to install Terraform
The official Terraform site has some pretty good instructions on how to install to terraform, however we have developed a strong preference to make use of the Homebrew package manager approach. In fact that is the approach we take internally without exceptions.
The first step is to ensure you reference the official Hashicorp tap, then install the package as detailed in the script below
brew tap hashicorp/tap
# install terraform
brew install hashicorp/tap/terraform
Once installed you can verify that Terraform has been installed by checking the version as follows
terraform --version
This should return the version details similar to the screen shot below
How to set up a Terraform alias on Linux
The following step is entirely optional, and if we perfectly honest is only for the really lazy developers who want to
save a few additional keystrokes a day. However, if your typing is as bad as mine, you find yourself having to type a
long word like terraform
difficult without making a typo then you can create a simple alias to help.
It is pretty standard on must Linux distros to have e a file in my home directory named .bash_aliases
which is use to
define all sorts of aliases on your machine. Aliases in Linux are pretty cool things to use, because they let you
define your own commands — or command shortcuts — so you can customize the command line and make it work the way you
want it to work.
If you take a look at your .bashrc
file you may most likely find the lines below that automatically loads the
bash_aliases
everytime you start a terminal session
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
This will enable you to do define an alias in the .bash_aliases
file as follows to make the tf
be the terraform
command. To do this you can simply execute the following commands in your terminal window
echo "alias tf='terraform'" | tee -a .bash_aliases
. ~/.bashrc
Install the DigitalOcean CLI
We will also need to install doctl the Digital Ocean Command Line Tool. allows you to interact with the DigitalOcean API via the command line. It supports most functionality found in the control panel. You can create, configure, and destroy Digital Ocean resources like Droplets, Kubernetes clusters, firewalls, load balancers, database clusters, domains, and more.
We mentioned previously in this article that you will need a Digital ocean account, you can get one if you haven't already by following this link and as a bonus you will get $200 free credit for 60 days, Sign up for DigitalOcean
Previously, we installed Terraform using Homebrew, it will come as no surprise then when we recommend using Homebrew
to install the DigitalOcean CLI, also known as doctl
. This can simply done by executing the following command in your
terminal window.
brew install doctl
Once the install has completed you should be able to verify that it was successful, and execute the following command and presented with a number options that are now available to you.
doctl
Add your access token to your Environment Variables
Although we have managed to login to DigitalOcean CLI with our access token, we still need to be able to share and
use our DigitalOcean token with terraform. To do this, we typically follow steps similar to what we did with the aliases,
in that we are going to create a .bash_variables
file to store our environment variables.
One reason we like to do this, is mostly to do with organization. I hate having to open my .bashrc
and try read through
it all to find a variable and change its values. I prefer to have a dedicated file for that, and therefore it is also
easier to back that file up with my variable values.
In the line below, we'll create and add our personal access token to .bash_variables
file.
echo "export DIGITALOCEAN_TOKEN=<paste your token here>" | tee -a .bash_variables
With file now in place, you can add the following lines to your .bashrc
file, to load the file if it exists.
if [ -f ~/.bash_variables ]; then
. ~/.bash_variables
fi
Top Tip
The Digital Ocean Terrafrom provider, automatically looks for and uses either of the following Environment Variables if they exist
DIGITALOCEAN_TOKEN
DIGITALOCEAN_ACCESS_TOKEN
So you don't have to pass them in when using terraform apply
Summary
At Threenine, we are a small yet dynamic team, we emphasize simplicity, efficiency, and agility, often deviating from textbook best practices to evolve our processes. We're constantly looking to optimize our workflow and have embraced Infrastructure as Code, specifically using Terraform for managing cloud infrastructure, especially with DigitalOcean.
This post provides instructions on installing and setting up Terraform on a Linux development machine, highlighting the advantages of using Homebrew for installation. Additionally, it guides on setting up a convenient Terraform alias and installing the DigitalOcean CLI (doctl). Steps for creating and managing environment variables for DigitalOcean tokens are also covered, emphasizing the ease and organization of using separate files for aliases and variables. The detailed instructions aim to streamline the setup process, making it easier for developers to efficiently manage infrastructure on DigitalOcean.
Back-end software engineer
Experienced software developer, specialising in API Development, API Design API Strategy and Web Application Development. Helping companies thrive in the API economy by offering a range of consultancy services, training and mentoring.