How to Install and Use Terraform on a Linux

terraform on linux

Terraform is a leading cloud orchestration tool that is completely free to use. Often referred to as an “Infrastructure as Code” tool, Terraform enables users to deploy infrastructure using code-based configurations.

Developed by HashiCorp, Terraform is an extremely popular open-source program licensed under the Mozilla Public License. It supports a range of cloud providers, including but not limited to Azure Cloud, AWS, Oracle Cloud, and GCP. Additionally, it allows users to build, modify, and version an infrastructure quickly and easily.

By utilizing machine-readable definition files, Terraform simplifies the provisioning and management of resources like Networks, Storage, Virtual Machines, and Databases.

Let’s explore how to install this tool and use it on Linux.

How to Install Terraform on Linux

To install Terraform, you will need a server running any Linux distro and the root password to the server.

Bear in mind that the CentOS and Ubuntu default repositories do not include Terraform in them. So, you must install the tool from its official repository.

To install Terraform on Debian- and Ubuntu-based distros, do these steps:

  1. Begin by installing the dependencies that the tool requires by running this command:
apt-get install software-properties-common gnupg2 -y
  • Now, you must import the Terraform key with this command:
curl -fsSL https://apt.releases.hashicorp.com/gpg | apt-key add –
  • You’re ready to add the Terraform repo to your machine. Do this by running the following:
apt-add-repository “deb [arch=$(dpkg –print-architecture)] https://apt.releases.hashicorp.com $(lsb_release -cs) main”
  • You’re not finished yet – you must update the repository before installing the tool. You can do both these tasks by running the following:
apt-get update -y apt-get install terraform -y
  • The above command should install Terraform on your machine. To verify the integrity of the installation, you must run:
terraform –version

The above command should return the version details of Terraform. If it does, you can be sure that Terraform has been installed correctly.

If you want to install Terraform on RHEL- or CentOS-based Linux distros, follow these steps:

  1. Install the tools and utilities that Terraform needs with this command:
yum install yum-utils -y
  • Add the Terraform repository to your machine with this command:
yum-config-manager –add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
  • Follow the best practice of updating the repository and then installing Terraform on your machine. You can accomplish both tasks with a single command:
yum update yum install terraform
  • Verify that the installation has been completed without errors by running:
terraform –version

If the command returns Terraform’s version details, the tool is installed on your machine correctly, and you’re ready to use it.

How to Use Terraform on Linux

As mentioned earlier, Terraform works with cloud services and helps you manage your infrastructure using code. To use Terraform, you must first give it access to your cloud service of choice.

For the sake of demonstration, we use AWS Cloud in this tutorial. To begin using the tool, you need to create a Terraform project. And this project needs to have a dedicated directory.

So, run the following command and create a directory for your project:

mkdir project cd project

Now, navigate into the newly created directory and create a configuration file with the .tf extension. Do this by running the following:

nano app.tf

When the file opens, add these lines to it:

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

Before closing the file, make sure you save it. Of course, you must replace the secret_key and access_key values with the AWS keys you use.

The next step involves initializing the Terraform tool. You can do this by running “terraform init” on the terminal. The output will indicate that the backend and provider plugins are initialized and that the cloud server has been installed.

Now, you need a Terraform plan to check the changes you need to make. You can create a plan with this command:

terraform plan

You will see an output indicating the changes required for your infrastructure to match the configuration. If the infrastructure and configuration match, the output will indicate no changes are needed.

Finally, you must connect the Terraform tool to your cloud service by starting the build with this command:

terraform apply

The output will indicate that the configuration has been applied. If you find the need to delete the provisions you have created, you can run this command:

terraform destroy

Terraform Commands All Users Should Know

Here’s a list of commands that will help you manage your infrastructure:

  • terraform refresh: This command updates the statefile of your infrastructure to ensure that it accurately reflects the metadata of the physical resources being tracked.
  • terraform taint: Use this command to mark a resource as tainted manually. This action forces a destroy and recreate process during the next plan execution.
  • terraform graph: Execute this command to generate a visual dependency graph of Terraform resources based on your configuration files. It provides a clear illustration of resource relationships.
  • terraform remote: This command allows you to configure remote state storage for your Terraform projects, enabling collaboration and centralized state management.
  • terraform get: Use this command to download and install modules necessary for your Terraform configuration, facilitating modularization and reusability.
  • terraform show: This command enables you to inspect either the Terraform state or the plan, providing detailed information about the current state of your infrastructure or the planned changes.

Conclusion

Terraform is an excellent infrastructure-as-code tool that doesn’t compromise on features.

After setting up your Terraform installation with this brief guide, look into installing the best Terraform tools to manage any complex infrastructure easily.