Terraform is a powerful tool used for building and managing virtual server infrastructures. It supports multiple cloud providers, allowing users to create and maintain their infrastructure using a unified language. If you’re interested in automating your work or exploring the world of infrastructure-as-code, Terraform is an excellent choice. In this guide, we will walk you through the process of installing Terraform on Red Hat Enterprise Linux (RHEL) compatible operating systems like AlmaLinux 8, CentOS 8, and Rocky Linux 8.
Prerequisites
Before we begin, let’s ensure that you have the necessary prerequisites in place:
- An RHEL 8 or later compatible operating system. For this tutorial, we will be using CentOS 8 as the base operating system.
- A non-root user with sudo privileges set up on the system.
Updating Your System
To ensure that your system is up to date, it’s essential to run regular updates. This not only improves performance but also enhances security. To update your system, open a terminal window and enter the following command:
sudo yum check-update&& sudo yum update -y
This command will check for available updates and install them. The process may take a while, depending on your server’s performance. Once the update is complete, you can proceed with the installation.
Installing Terraform on CentOS 8
CentOS 8 does not include the Terraform package in its repository. Therefore, we need to add the HashiCorp repository using the yum-utils package and then install Terraform using the yum package manager.
Adding the HashiCorp Repository
To add the HashiCorp repository to your system, run the following command:
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
This command configures your system to access the HashiCorp repository.
Verifying the Repository
To verify if the HashiCorp repository has been added successfully, run the following command:
sudo dnf repolist
You should see the HashiCorp repository listed in the output.
Installing Terraform
Now that the repository is set up, we can proceed with the installation. Run the following command to install Terraform:
sudo yum install terraform -y
This command will download and install all the required packages from the repository. The installation process may take some time, depending on your internet connection speed. Once the installation is complete, you can verify the version of Terraform installed by running the following command:
terraform version
If everything was installed correctly, you should see the Terraform version displayed in the output.
Getting Started with Terraform
Now that Terraform is installed on your system, you can begin using it to create and manage your infrastructure. Let’s walk through the process of creating a simple AWS EC2 instance using Terraform.
Configuring AWS Credentials
Before we can start provisioning resources on AWS, we need to configure our AWS credentials. Terraform uses environment variables to access your AWS access key and secret access key. Open a terminal window and enter the following commands:
export AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY export AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY
Replace YOUR_ACCESS_KEY
and YOUR_SECRET_ACCESS_KEY
with your own AWS credentials.
Creating a Terraform Configuration File
Terraform uses a file called main.tf
to define your infrastructure. This file contains the configuration details for your AWS EC2 instance. Create a new file called main.tf
in a directory of your choice and open it in a text editor.
In the main.tf
file, add the following code:
provider "aws" { region = "us-east-1" } resource "aws_instance" "example" { ami = "ami-0c94855ba95c71c99" instance_type = "t2.micro" }
This code defines an AWS provider and an EC2 instance resource. It specifies the region as us-east-1
, selects an Amazon Machine Image (AMI) with the ID ami-0c94855ba95c71c99
, and sets the instance type to t2.micro
. Feel free to modify these values to suit your requirements.
Initializing Terraform
Before we can apply our configuration and create the EC2 instance, we need to initialize Terraform. In your terminal window, navigate to the directory containing the main.tf
file and run the following command:
terraform init
This command initializes the Terraform working directory, downloads the necessary provider plugins, and sets up the backend.
Applying the Terraform Configuration
Once Terraform is initialized, we can apply our configuration and create the EC2 instance. Run the following command:
terraform apply
Terraform will display a plan of the changes it will make to your infrastructure. Review the plan, and if everything looks correct, type yes
to proceed with the deployment. Terraform will then provision the EC2 instance based on the configuration specified in the main.tf
file.
Managing Your Infrastructure
Terraform not only allows you to create new resources but also provides the ability to manage existing resources. To modify an existing resource, make the necessary changes to your main.tf
file and run the terraform apply
command again. Terraform will apply the changes and update your infrastructure accordingly.
Cleaning Up
Once you’re finished experimenting with Terraform, it’s essential to clean up your resources to avoid unnecessary costs. To destroy the resources created by Terraform, run the following command:
terraform destroy
Terraform will display a plan of the resources to be destroyed. If everything looks correct, type yes
to proceed with the deletion. Terraform will then destroy the resources and remove them from your infrastructure.
Conclusion
In this tutorial, we have learned how to install and configure Terraform on RHEL compatible operating systems like AlmaLinux 8, CentOS 8, and Rocky Linux 8. We walked through the process of creating an AWS EC2 instance using Terraform and explored some basic Terraform commands for managing your infrastructure. Terraform offers a powerful and flexible way to automate your infrastructure provisioning and management tasks, giving you the ability to scale and evolve your applications with ease.
If you’re looking for reliable and scalable cloud hosting solutions, consider Shape.host. With Shape.host’s Linux SSD VPS, you can take advantage of the performance benefits of solid-state drives and the flexibility of virtual private servers. Shape.host offers a range of plans to suit different needs and budgets, making it an excellent choice for businesses of all sizes. Harness the power of Terraform and Shape.host to unlock the full potential of your infrastructure.