Introduction

Terraform is an open-source infrastructure automation command-line tool created by HashiCorp written in the Go programming language. It allows you to define and describe your infrastructure as code inside configuration files using a declarative language and to deploy and manage that infrastructure across a variety of public cloud providers, private cloud and virtualization platforms. This practice is known as Infrastructure as Code (IaC). Your goal is to describe the infrastructure you want and Terraform will figure out how to create it. Terraform can interact with many different public cloud platforms, including Vultr. In the official Terraform documentation, these cloud platforms and services are referred to as providers. The official Terraform provider for Vultr is natively available since October 2019.

Instead of manually creating infrastructure via the Vultr Control Panel or running dozens of commands by using various CLI tools to interface with the Vultr API, you can utilize Terraform to describe your infrastructure and deploy it with just a few commands.

This guide will explain to you how to install Terraform on macOS and get you up and running with Terraform.

Infrastructure as Code (IaC) and its benefits

You may ask yourself why treat infrastructure as code is important and what problems it solves. Here are some of the benefits of Infrastructure as Code approach and using Terraform:

  • Easy collaboration and configuration sharing via common software development collaboration tools like Git and Github.
  • Evolve and version your infrastructure. Because your resources are declared in code, you can track changes to that code over time in version control systems like Git.
  • Automated provisioning - you manage your infrastructure not by clicking around a web page or manually executing shell commands, but through code.
  • Consistent, repeatable workflow.
  • Reproducible production, staging, and development environments. Terraform makes it easy to re-use configurations for similar infrastructure, helping you avoid mistakes and save time.

Install Terraform on macOS Catalina

Terraform is very easy to install and can run on your desktop or a remote server. You can download Terraform from the Terraform download page. Select the appropriate package for your operating system and architecture (on macOS, you only have one option - 64-bit), click the download link to download the ZIP archive, and unzip it into the directory where you want Terraform to be installed. The archive will extract a single binary called terraform, which you’ll want to add to your PATH environment variable. Alternatively, Terraform might be available in your operating system’s package manager; for example, you can run brew install terraform on macOS if you have Homebrew installed. You can also install Terraform inside a Docker container.

First, you may need to set your PATH environment variable so that it includes ~/bin directory. Linux power users typically add ~/bin directory to PATH, but you can add whichever directory you prefer to put terraform binary.

echo "export PATH=$PATH:$HOME/bin" >> ~/.bash_profile

Note: PATH is an environment variable that sets command search directories. It is a list of directories that should be searched anytime a user types a command. You can check your PATH with echo $PATH on the command line.

Download the Terraform ZIP archive. Visit the Terraform home page for the latest version numbers.

curl -O -J -L https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_darwin_amd64.zip

Unzip the archive. The archive will extract a single binary called terraform.

unzip terraform_0.12.24_darwin_amd64.zip

Move the terraform binary to a directory included in your system's PATH in our case that's ~/bin directory.

mv terraform ~/bin

To check whether things are working, run the terraform version command, and you should see the version number.

terraform version
# Terraform v0.12.24

The Vultr Provider

The Terraform provider for Vultr was officially released in 2019. The Vultr provider can be used to create Vultr instances, Bare metal, Images, DNS domain records, Block Storage Volumes, and other resources. Terraform’s official Vultr provider documentation details each resource that can be created and managed.

Deploy a Single VC2 Server

Terraform code is written in the HashiCorp Configuration Language (HCL) in files with the extension .tf. It is a declarative language, which is used to describe the infrastructure you want and Terraform will figure out how to create it. You can write Terraform code in any text editor. In this part, we will deploy a single Vultr Cloud Compute (VC2) instance using Terraform.

First, create an empty folder and cd into it:

mkdir terraform_sample
cd terraform_sample

Create a file in it called main.tf:

touch main.tf

In your ~/.bash_profile you need to set export VULTR_API_KEY="Your Vultr API Key". Beforehand, you need to enable API access for your Vultr account in Vultr Control Panel. You can do that at https://my.vultr.com/settings/#settingsapi.

# Put this in ~/.bash_profile
export VULTR_API_KEY="your-vultr-api-token"

The first step to using Terraform is typically to configure the provider(s) you want to use. Add the following contents to main.tf file.

provider "vultr" {
  
}

This tells Terraform that you are going to be using Vultr as your provider.

For each type of provider, there are many different kinds of resources that you can create, such as servers, databases, caches, load balancers, firewalls, and all other parts of your infrastructure.

resource "<PROVIDER>_<TYPE>" "<NAME>" {
  [CONFIG ...]
}

The following code deploys Vultr Cloud Compute (VC2) instance and it will use Ubuntu 18.04 as the operating system in the Frankfurt location.

resource "vultr_server" "my_server" {
  region_id = "9"
  os_id = "270"
  plan_id = "201"
  enable_ipv6 = false
  auto_backup = false
  ddos_protection = false
  enable_private_network = false 
  notify_activate = false
  label = "myLabel"
  hostname = "myHostname"
  tag = "myTag"
}

The final version of main.tf should resemble something like this:

provider "vultr" {
  # In your ~/.bash_profile you need to set
  # export VULTR_API_KEY="Your Vultr API Key"
}

resource "vultr_server" "my_server" {
  region_id = "9"
  os_id = "270"
  plan_id = "201"
  enable_ipv6 = false
  auto_backup = false
  ddos_protection = false
  enable_private_network = false 
  notify_activate = false
  label = "myLabel"
  hostname = "myHostname"
  tag = "myTag"
}

Note: Check out terraform-datalib-vultr Terraform module by Vultr. This module removes the need to query the public API endpoints to find App/OS/Region ID’s.

In a terminal, go into the folder where you created main.tf and run the terraform init command. This command downloads Terraform providers or make sure that they already exist. init must be run on any Terraform directory.

terraform init

Next, run terraform plan command. plan will display in the console all the changes that it's going to make.

terraform plan

To create the Instance, run the terraform apply command:

terraform apply

Congrats, you’ve just deployed a Vultr Cloud Compute (VC2) instance in your Vultr account using Terraform. To verify this, head over to the Vultr Control Panel and you should see Ubuntu 18.04 server (1 CPU, 1GB RAM,25GB SSD) in Frankfurt location provisioned via Terraform.

To remove this server, run:

terraform destroy

Conclusion

You now have a basic grasp of how to use Terraform and I hope you recognize the benefits of developing infrastructure as code. The declarative language makes it easy to describe exactly the infrastructure you want to create. The plan command allows you to verify your changes and catch bugs before deploying them. There is a lot more to learn about Terraform and the best place for that is Terraform docs.