How to install Terraform on Ubuntu Server

How to install Terraform on Ubuntu Server

If you’re looking to add automation into your Kubernetes pipeline, you might need Terraform. Find out how to install this must have for CI/CD.

istock-918951042.jpg

Image: iStockphoto/monsitj

With Kubernetes, there’s very little you cannot do. From deploying, scaling, managing, and developing, you are in control of how things happen, when they happen, and why they happen. Kubernetes has another trick up it’s enterprise-ready sleeve: Automation. With automation you can ensure your Continuous Integration/Continuous Delivery (CI/CD) pipelines run seamlessly and timely.

But how do you add automation into the Kubernetes chain of command? With Terraform. This open source infrastructure as code tool was created by HashiCorp and enables users to define and provision data center infrastructure using either HashiCorp’s own high-level configuration language or JSON.

But before you can integrate Terraform into your CI/CD pipeline, you have to first install it. Let me show you how.

SEE: DevOps: A cheat sheet (TechRepublic)

What you’ll need

The first thing you’ll need to install Terraform is a running Kubernetes cluster. If you don’t already have that cluster up and running, walk through my tutorial: How to deploy a Kubernetes cluster on Ubuntu server.

You’ll also need a user with sudo privileges. 

How to install Terraform

The first thing to be done is the installation of Terraform. To do this, we must locate the latest version of the software available from the Terraform web site. As of this writing, that is 0.12.24. 

Download the latest version with the command:

wget https://releases.hashicorp.com/terraform/0.12.24/terraform_0.12.24_linux_amd64.zip

If you find the latest version to be newer than 0.12.24, make sure to substitute the new release number in the wget command.

Install zip with the command:

sudo apt-get install zip -y

Next, unzip the Terraform download with the command:

unzip terraform*.zip

Finally, move the executable with the command:

sudo mv terraform /usr/local/bin

Test to make sure the installation works with the command:

terraform version

You should see Terraform v0.12.24 printed out in the terminal window.

How to initialize Terraform

Now that terraform is installed, it must be initialized. However, to do that we must configure a provider, otherwise it won’t know where to pull from. We’re going to simply use Kubernetes as the provider for this instance. 

First, create a new directory to work in with the command:

mkdir ~/terraform

Change into that new directory with the command:

cd ~/terraform

Create a new configuration file with the command:

nano config.tf

In that file, paste the following:

# main.tf
provider "kubernetes" {}

Save and close the file. 

Now that we’ve defined our provider, we can initialize Terraform with the command:

terraform init

At this point, Terraform will download the plugin for the Kubernetes provider and you’re ready to go. 

If you’re looking for some AWS provider examples, some great samples can be be had with the command:

git clone https://github.com/terraform-providers/terraform-provider-aws.git

Change into the newly cloned directory with the command:

cd terraform-provider-aws/examples

You can now comb through much more complicated provider configurations for AWS. Change into one of the example directories (such as the two-tier directory) and then initialize that provider with the command:

terraform init

Once Terraform has initialized, you’ll want to go through the configurations for that example, in order to successfully run the the command:

terraform apply

For example, for the AWS providers, you’ll need to supply legitimate authentication keys for an AWS account–otherwise the apply command will fail.

I highly recommend you take the time to examine all the sample terraform provider configurations within the terraform-provider-aws directory, as there’s a lot to take in. 

Although Terraform isn’t the easiest tool you’ll ever use, the time it takes to understand its usage will be well spent. If you’re looking to integrate automation into your pipelines, this might well be exactly what you’re looking for.

Also see

Source of Article