How to Install and Use Terraform on Linux
How to Install and Use Terraform on Linux
Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp that enables you to define, provision, and manage infrastructure across multiple cloud providers. It supports a wide range of cloud platforms like AWS, Azure, Google Cloud, and on-premise data centers, allowing you to create and manage infrastructure through declarative configuration files. In this guide, we’ll explore how to install Terraform on a Linux system and cover its basic usage to help you get started with automating your infrastructure.
Prerequisites
Before installing Terraform, ensure that your system meets the following requirements:
A Linux distribution like Ubuntu, CentOS, or Fedora.
Access to the terminal with sudo privileges.
An internet connection for downloading the necessary packages.
Step 1: Installing Terraform
Terraform is distributed as a binary executable, and HashiCorp provides a simple method to install it on Linux.
Option 1: Using the Official HashiCorp Repository (Recommended for Ubuntu/Debian)
Follow these steps to install Terraform using the official HashiCorp repository:
Update your package manager:
sudo apt update
Install the necessary dependencies for HTTPS:
sudo apt-get install -y gnupg software-properties-common
Add the HashiCorp GPG key:
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg –dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
Add the HashiCorp official repository:
echo “deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main” | sudo tee /etc/apt/sources.list.d/hashicorp.list
Update the package manager again to include the new repository:
sudo apt update
Install Terraform:
sudo apt-get install terraform
Option 2: Installing Terraform on CentOS/RHEL/Fedora
If you’re using a Red Hat-based distribution like CentOS or Fedora, follow these steps:
Install the Yum-config-manager to manage repositories:
sudo yum install -y yum-utils
Add the HashiCorp repository:
sudo yum-config-manager –add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
Install Terraform:
sudo yum install -y terraform
Option 3: Manual Installation (For Any Linux Distribution)
If your distribution doesn’t have a package manager that supports the HashiCorp repository, you can manually install Terraform:
Download the latest version of Terraform: Go to the official Terraform downloads page and download the appropriate binary for your system using wget or curl. For example:
wget https://releases.hashicorp.com/terraform//terraform__linux_amd64.zip
Unzip the downloaded file:
sudo unzip terraform__linux_amd64.zip -d /usr/local/bin/
Verify the installation: Run the following command to check if Terraform was successfully installed:
terraform –version
Step 2: Basic Terraform Commands and Configuration
Now that you have Terraform installed, it’s time to learn how to use it. Terraform works by using configuration files written in HashiCorp Configuration Language (HCL) to define your infrastructure. Here’s a simple example to demonstrate the key steps in using Terraform.
Step 1: Create a Directory for Your Terraform Project
Navigate to a directory where you want to store your Terraform files and create a new folder:
mkdir terraform-project
cd terraform-project
Step 2: Create a Terraform Configuration File
Create a new configuration file, typically with a .tf extension (e.g., main.tf). For this guide, we’ll use an example that provisions an AWS EC2 instance. The configuration file will look something like this:
provider “aws” {
region = “us-west-2”
}resource “aws_instance” “example” {
ami = “ami-0c55b159cbfafe1f0”
instance_type = “t2.micro”
}
This configuration tells Terraform to use AWS as the cloud provider and to create an EC2 instance using the specified AMI and instance type.
Step 3: Initialize the Terraform Project
Before Terraform can provision your infrastructure, you must initialize it. This process downloads the required provider plugins (in this case, AWS):
terraform init
After initialization, Terraform is ready to plan and apply your changes.
Step 4: Plan and Apply the Configuration
The next step is to plan the changes. Terraform will output the actions it will perform without actually applying them:
terraform plan
If the plan looks good, apply the changes to provision the resources:
terraform apply
You’ll be asked to confirm by typing “yes”. After a few moments, Terraform will create the EC2 instance based on your configuration.
Step 5: Destroying Infrastructure
When you no longer need the infrastructure, you can destroy it using the following command:
terraform destroy
This will remove all resources created by the configuration file. Like the apply command, Terraform will ask you for confirmation before proceeding.
Step 3: Managing Terraform State
Terraform keeps track of the infrastructure it manages using a state file (terraform.tfstate). This state file is critical because it records the current state of your infrastructure and is referenced when Terraform plans and applies changes. By default, the state file is stored locally in your project directory.
Remote State Storage
If you’re working with a team or managing large infrastructures, it’s a best practice to store your state remotely using backends like AWS S3 or HashiCorp Consul. This ensures that the state is safely stored and accessible to all team members:
terraform {
backend “s3” {
bucket = “my-terraform-state”
key = “path/to/my/key”
region = “us-west-2”
}
}
By using a remote backend, you reduce the risk of losing your state file and enable collaboration among team members, as everyone can work with the same infrastructure state.
Step 4: Terraform Modules
As your Terraform configurations grow, reusability becomes important. Terraform modules allow you to organize and reuse your code. A module is simply a set of Terraform files organized in a directory. You can call these modules within your configurations like this:
module “ec2_instance” {
source = “./modules/ec2”
instance_type = “t2.micro”
}
Using modules helps you break down complex configurations and improve code maintainability.
Thank you for reading our article! If you’re interested in learning more about Linux systems, Packer, and Vagrant, feel free to check out the link below. 🙂
How to Install Packer on Arch Linux
How to Install Vagrant on Ubuntu
If you’re aiming to improve your server skills, explore these packages by selecting a reliable and suitable server from our site. Good luck! 🙂