Terraform is an infrastructure-as-code tool that allows you to provision and manage infrastructure in any cloud created by HashiCorp. It provides a rich interface for orchestrating single or multi-cloud deployments. Using Terraform, you can provision all the infrastructure components in code. If you are looking for a tool to manage infrastructure components, such as computing, storage, and networking resources, then Terraform is the best tool.
This post will show you how to install Terraform on Fedora Linux.
Step 1 – Install Terraform from Hashicorp Repo
By default, the Terraform package is unavailable in the Fedora default repo, so you will need to install it from the HashiCorp repo.
First, update the server and install the HashiCorp repo with the following commands.
dnf update -y
dnf install -y dnf-plugins-core dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo
Next, verify the added repo with the following command.
dnf repolist
You will see the following output.
repo id repo name fedora Fedora 34 - x86_64 fedora-cisco-openh264 Fedora 34 openh264 (From Cisco) - x86_64 fedora-modular Fedora Modular 34 - x86_64 hashicorp Hashicorp Stable - x86_64 updates Fedora 34 - x86_64 - Updates updates-modular Fedora Modular 34 - x86_64 - Updates
Next, install Terraform using the following command.
dnf install terraform -y
After the installation, verify the Terraform version with the following command.
terraform --version
Output:
Terraform v1.5.7 on linux_amd64
Step 2 – Install Terraform Manually
You can also install Terraform manually by downloading it from GitHub. Run the following command to download the latest version of Terraform.
TER_VER=`curl -s https://api.github.com/repos/hashicorp/terraform/releases/latest | grep tag_name | cut -d: -f2 | tr -d \"\,\v | awk '{$1=$1};1'` wget https://releases.hashicorp.com/terraform/${TER_VER}/terraform_${TER_VER}_linux_amd64.zip
Once the download is completed, unzip the downloaded file.
unzip terraform_${TER_VER}_linux_amd64.zip
Next, move the Terraform binary to the system location.
mv terraform /usr/bin/
Next, verify the Terraform version using the following command.
terraform --version
You will see the Terraform version in the following output.
Terraform v1.5.7 on linux_amd64
Step 3 – How to Use Terraform
You can see all available options for Terraform using the following command.
terraform
You will see the following output.
Main commands: init Prepare your working directory for other commands validate Check whether the configuration is valid plan Show changes required by the current configuration apply Create or update infrastructure destroy Destroy previously-created infrastructure All other commands: console Try Terraform expressions at an interactive command prompt fmt Reformat your configuration in the standard style force-unlock Release a stuck lock on the current workspace get Install or upgrade remote Terraform modules graph Generate a Graphviz graph of the steps in an operation import Associate existing infrastructure with a Terraform resource login Obtain and save credentials for a remote host logout Remove locally-stored credentials for a remote host metadata Metadata related commands output Show output values from your root module providers Show the providers required for this configuration refresh Update the state to match remote systems show Show the current state or a saved plan state Advanced state management taint Mark a resource instance as not fully functional test Experimental support for module integration testing untaint Remove the 'tainted' state from a resource instance version Show the current Terraform version workspace Workspace management
Now, let’s create a new project and see how to use Terraform to manage infrastructure.
mkdir projects
Next, create a Terraform main configuration file inside the project directory.
cd projects nano main.tf
Add your cloud provider’s information as shown below.
provider "aws" { access_key = "" secret_key = "" region = "us-west-1" }
Save and close the file, then initialize a Terraform working directory using the following command.
terraform init
You will see the following output.
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v5.16.2... - Installed hashicorp/aws v5.16.2 (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.
Next, generate an execution plan using the following command.
terraform plan
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.
Now, run the following command to build your infrastructure.
terraform apply
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 destroy your infrastructure, run the following command.
terraform destroy
Conclusion
In this post, we explained different ways to install Terraform on Fedora Linux. We also showed you how to use Terraform to deploy and manage infrastructure. You can now deploy and use Terraform on dedicated server hosting from Atlantic.Net!