Linux

How to Install Terraform on Linux

TERRAFORM_ON_LINUX_FEATURE_IMAGE

HashiCorp’s Terraform is an open-source infrastructure automation tool. The Go programming language was used to create it. It enables you to define and describe your infrastructure as code within configuration files using a declarative language, as well as deploy and manage that infrastructure across a range of public cloud providers like as AWS, GCP, and Azure. Other Guides can be found here: How to install Apache Tomcat on Ubuntu/ and How to Install LAMP Stack on Ubuntu 18.04/ and How to install MariaDB on Ubuntu/ and How to Configure Advanced PAM in Linux/ also Practical use of SELinux in production/ and How to create and deliver a report on System Utilization on a Linux based OS

Terraform is an infrastructure as code (IaC) tool that allows you to build, change, and version infrastructure safely and efficiently. 

Benefits of using Terraform

  • Infrastructure as code (IaC) solutions allow you to manage infrastructure without using a graphical user interface by using configuration files. By defining resource configurations that you can version, reuse, and share, IaC allows you to develop, change, and manage your infrastructure in a secure, consistent, and repeatable manner.
  • HashiCorp’s infrastructure as code tool is Terraform. It maintains the lifespan of your infrastructure and allows you define resources and infrastructure in human-readable, declarative configuration files. Using Terraform instead of manually maintaining your infrastructure has numerous benefits:
  • Terraform can manage infrastructure across a variety of cloud platforms. The human-readable configuration language aids in the rapid development of infrastructure code. Terraforms state feature allows you to keep track of resource changes as they happen throughout your deployments. To cooperate on infrastructure safely, you can commit your configurations to version control.
  • Automated infrastructure management, lower deployment costs, and reduced provisioning time

To use terraform, you need to download from source and install it, it is distributed as a binary package. In this guide, I will walk you through a few short steps on how to install terraform on your Linux machine, whether you use Centos, RHEL or Ubuntu, the procedure is the same.

Step1: Retrieve the terraform binary by downloading a pre-compiled binary or compiling it from source with the command:

wget https://releases.hashicorp.com/terraform/1.1.2/terraform_1.1.2_linux_amd64.zip
terraform112

Step2: Install zip with the command:

sudo apt-get install zip -y
zip

Step3: unzip the Terraform download with the command:

unzip terraform*.zip
zipter

Step4: move the executable with the command:

sudo mv terraform /usr/local/bin
move

Step5(optional): check the version of terraform downloaded

Terraform --version
terraform-version

First Steps after installation:

  1. Create a directory, we decided our directory name is terraform as shown below
mkdir  ~/terraform

2. Change into the newly created directory

cd ~/terraform

3. Create a new configuration file with the command:

vim config.tf

let’s paste this sample provider information into the config file we just created

provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
}
config.tf_

Save and close the file .

more info about the provider can be gotten here: https://registry.terraform.io/providers

4. Initialize Terraform

Terraform init
terraform-init

Demo

Deploy a resource to AWS: The first thing to do will be to install aws-cli

sudo snap install aws-cli --classic
aws-cli
aws cli

We will also set up the AWS CLI with credentials from our aws account by running the command below:

aws configure

You will be prompted for security details as shown below:

awsconfigure

Next thing would be to change into the directory we created earlier and create a main.tf file. It will be wise to comment out the provider we wrote earlier so that when we call init, it ignores that one.

cd ~/terraform
touch main.tf

Next, we open main.tf in a text editor and add a block for terraform, provider & the resource we are creating

vim main.tf
s3-bucket

Our objective for this exercise is to make that that an s3 bucket defined in the main.tf file is created in our aws console.

terraform init
terraform-init1
init
terraform plan
terraform-plan
plan
plan
terraform apply
terraform-apply-completed-1
apply
created
console output

As we can see from the screenshot above, the deployment was successful. Let’s try something else that is interesting, to show you that terraform is cloud agnostic. We shall be using terraform to create a resource group in Azure.

The steps are as shown below, you will use the Azure CLI tool to authenticate with Azure.

Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList '/I AzureCLI.msi /quiet'; rm .\AzureCLI.msi
az login
mkdir terraform && cd $_
code main.tf
code main.tf will take your directly to visual studio code. When you are there just past the code
provider "azurerm" {
  version = "1.38.0"
}

#create resource group
resource "azurerm_resource_group" "rg" {
    name     = "rg-MyFirstTerraform"
    location = "westus"
    tags      = {
      Environment = "Terraform Demo"
    }
}
terraform-tda

Terraform Plan

terraform-planazure
terraform plan

Terraform apply

terraform-apply-completed

let’s check the azure platform for the resource group that we deployed with terraform. You can see that the ID matches that of the one in Visual studio code

deploy-resource
console output

Summary

Installing Terraform on any system is not difficult, whether it’s aws or azure you are deploying to, the steps are same. In this guide I walked you through how to install it on Ubuntu and how to create your directory and config files before initializing , I deployed to AWS and also deployed to Azure.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x