SnapShooter Learning Center

How to Install and Use Terraform on a Linux

How to Install and Use Terraform on a Linux

Simon Bennett]
Simon Bennett
Last Updated: Sep 28, 2021
Table of Contents

# Introduction

Terraform is a free, open-source and one of the most popular cloud orchestration tools. It is also known as an "Infrastructure as Code" because it is used to deploy your infrastructure through the code. It is developed by Hashicorp and released under Mozilla Public License. It supports a lot of cloud providers including, AWS, Azure Cloud, GCP, Oracle Cloud and more. Terraform allows you to build, change and versioning infrastructure safely, and efficiently. Terraform makes it easier to provision and manage resources like Virtual Machine, Storage, Network, and Database via machine-readable definition files.

Features

  • Free and Open-source.
  • Simple, lightweight and easy to use.
  • Declarative syntax.
  • Pluggable Modules.
  • Immutable infrastructure.

In this post, we will show you how to install and use Terraform on Linux.


# Requirements

  • A server running Linux operating system.
  • A root password is set up on your server.

# Install Terraform on Linux

By default, Terraform has not included in the Ubuntu or CentOS default repository so you will need to install it from their official repository.

For Ubuntu and Debian based distributions, follow the below steps to install Terraform:

First, install the required dependencies using the following command:

apt-get install software-properties-common gnupg2 -y

Next, import the Terraform key with the following command:

curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add -

Next, add the Terraform repository using the following command:

apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main"

Once the repository is added, update the repository and install the Terraform using the following command:

apt-get update -y
apt-get install terraform -y

Once the Terraform is installed, verify the installed version of Terraform with the following command:

terraform --version

You should get the following output:

Terraform v1.0.7
on linux_amd64

For CentOS and RHEL based distributions, follow the below steps to install Terraform:

First, install required tools using the following command:

yum install yum-utils -y

Next, add the Terraform repository using the following command:

yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo

Once the repository is added, update the repository and install the Terraform using the following command:

yum update
yum install terraform

After installing Terraform verify the Terraform version with the following command:

terraform --version

You should get the following output:

Terraform v1.0.7
on linux_amd64

# How to Use Terraform

Generally, Terraform is used to manage your infrastructure through code. Before using Terraform, you will need to provide access to cloud services like Azure, Amazon Web Services and others. In this section, we will use AWS Cloud providers.


# Create a Terraform Project

First, create a directory for your project with the following command:

mkdir project
cd project

Next, change the directory to your project and create a configuration file with .tf extension.

nano app.tf

Add the following lines:

provider "aws" {
  region     = "us-west-2"
  access_key = "access_key"
  secret_key = "secret_key"
}

Save and close the file when you are finished.

Note: Replace access_key and secret_key with your own AWS keys.


# Initialize Terraform

Next, initialize the Terraform using the following command:

terraform init

You should get the following output:

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.59.0...
- Installed hashicorp/aws v3.59.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

# Create a Terraform Plan

Next, you will need to create a Terraform plan to check what changes would be made.

You can do it with the following command:

terraform plan

You should get the following output:

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

# Connect Terraform to Cloud Service

If you are happy with the changes it is claiming to make, then you will need to execute terraform apply to commit and start the build.

terraform apply

Once you are done, you should get the following output:

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

If you want to delete all the provisioning that was created, run the following command:

terraform destroy

# Terraform Basic Commands

In this section, we will show you some basic commands that will help you to provision and manage your infrastructure.

  • terraform plan - Generates an execution plan for Terraform.
  • terraform apply - Builds or changes infrastructure according to Terraform configuration files.
  • terraform refresh - Update the statefile of your infrastructure with metadata that matches the physical resources they are tracking.
  • terraform destroy - Remove your Terraform managed infrastructure.
  • terraform taint - Manually mark a resource as tainted, forcing a destroy and recreate on the next plan.
  • terraform graph - Draw the nice visual dependency graph of Terraform resources according to configuration files.
  • terraform remote - Configure remote state storage.
  • terraform get - Download and install modules for the configuration.
  • terraform show - Inspect Terraform state or plan.

# Conclusion

In this guide, we explained how to install Terraform on Debian and RPM-based distributions. We also explained how to provision your infrastructure with Terraform. You can now start working with Terraform.


Did you find this article helpful?