How to Install Terraform on Debian 12

In this guide, we will show you how to install Terraform on Debian 12 system step-by-step.

Terraform is an open-source powerful tool for automating the deployment and management of infrastructure on public and private cloud. It is also Known as infrastructure as code (IaC) tool. If you are running Debian 12 and eager to harness the capabilities of Terraform, then you are in the right place.

Prerequisites

  • Pre-Install Debian 12
  • Sudo User with admin rights
  • Internet Connectivity

Installing Terraform on Debian 12

Terraform Debian package is not available in the default package repositories of Debian 12. So, for its installation, we will add official hashicorp apt repository.

1) Add Hashicorp APT Repository

Open the terminal and run following wget and echo command to download gpg key and add apt repository respectively.

$ wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
$ 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

Add-HashiCorp-Terraform-APT-Repository-Debian12

2) Install Terraform on Debian 12 with Apt Command

After adding the Terraform apt repository, we are good to install. Run following apt commands.

$ sudo apt update
$ sudo apt install terraform -y

This command will install latest version of terraform along with all the required dependencies.

Install-Terraform-on-Debian12-Apt-Command

Post terraform installation, check its version, run following terraform command.

$ terraform version
Terraform v1.6.4
on linux_amd64
$

3) Validate Terraform Installation

In order to verify your terraform installation, we will create a demo terraform project and main configuration files under this project. Before jumping into validation, let understand the terraform project files.

  • Main Configuration File (main.tf): In this file, we define the resources and settings for our infrastructure.
  • Variable Declaration Files (variables.tf) : In this file, you can declare input variables and set their default values. Input variables allow you to parameterize your Terraform configuration, making it more flexible.
  • Output Declaration Files (outputs.tf): This file is used to declare output values that can be useful for other Terraform configurations or scripts.
  • Terraform Provider Configuration Files (providers.tf): In larger projects or environments with multiple providers, you might use a separate file to configure your providers.
  • Terraform State Configuration Files (backend.tf): This file is used to configure the backend where terraform stores its state. State files keep track of the current state of your infrastructure. Common backends include Amazon S3, Azure Storage, or HashiCorp Terraform Cloud.

Create a demo project with name “terraform_demo” using mkdir command.

$ mkdir terraform_demo
$ cd terraform_demo/

Next, create main.tf file with following content.

$ vi main.tf
resource "local_file" "linux-distros" {
  filename = "/tmp/distros.txt"
  content = "There are multiple Linux distros like Ubuntu. Debian, RHEL, Rocky Linux & Kali Linux etc"
}

Save and exit the file.

In the same directory where your main.tf file is located, initialize the Terraform project, run below command.

$ terraform init

Terraform-Initialization-Command-Debian12

This command initializes the working directory and downloads the provider plugin (in this case, the local_file provider).

Next run ‘terraform plan’ command to see what exactly resources will be created. It is more of a dry run.

Terraform-Plan-Command-Output-Debian12

If you are satisfied with the plan and then go ahead and apply the configuration to create resources. Execute “terraform apply” command as shown below:

$ terraform apply

This will prompt you to confirm the action, type “yes” and hit enter.

Terraform-Apply-Command-Output-Debian12

Once you have verified that terraform is working as expected, you can destroy the resources to clean up:

$ terraform destroy

Terraform will again prompt you to confirm the destruction of resources. Type yes and press Enter.

Terraform-Destroy-Command-Output-Debian12

That’s all from this guide, we believe you have found it informative and useful. Feel free to post your queries and feedback in below comments section.

Also Read: How to Install VirtualBox on Debian 12 Step-by-Step

Leave a Comment

Your email address will not be published. Required fields are marked *