TerraformPilot

Terraform

How to Install Terraform on Amazon Linux 2023 - Complete Step-by-Step Guide

Step-by-step guide to install Terraform on Amazon Linux 2023. Install HashiCorp Terraform using dnf, verify the installation, and configure your first project.

LLuca Berton3 min read
How to Install Terraform on Amazon Linux 2023 - Complete Step-by-Step Guide

Introduction

#

Terraform is the industry-standard Infrastructure as Code (IaC) tool for provisioning and managing cloud resources. This guide shows you how to install Terraform on Amazon Linux 2023 in under 5 minutes.

Prerequisites

#

Before you begin, make sure you have:

  • A Amazon Linux 2023 system (physical or virtual machine)
  • Root or sudo access
  • An active internet connection
  • A terminal/shell session

Install Terraform on Amazon Linux 2023

#

Step 1: Add the HashiCorp Repository

#

HashiCorp provides official packages for major Linux distributions. Using the official repository ensures you always get the latest stable version with security updates.

sudo dnf install -y dnf-plugins-core
 
sudo dnf config-manager addrepo --from-repofile=https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
 
sudo dnf install terraform

Step 2: Verify the Installation

#

After installation, verify that Terraform is available:

terraform -version

You should see output similar to:

Terraform v1.12.x
on linux_amd64

Step 3: Enable Tab Completion (Optional)

#

Enable shell autocompletion for faster workflow:

terraform -install-autocomplete

Restart your shell or run source ~/.bashrc to activate.

Alternative: Install from Binary

#

If you prefer not to use a package manager, you can download the binary directly:

# Download the latest version
TERRAFORM_VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | grep -oP '"current_version":"\K[^"]+')
wget "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip"
 
# Extract and install
unzip "terraform_${TERRAFORM_VERSION}_linux_amd64.zip"
sudo mv terraform /usr/local/bin/
rm "terraform_${TERRAFORM_VERSION}_linux_amd64.zip"
 
# Verify
terraform -version

For ARM / Graviton instances (t4g, m7g, c7g), download the linux_arm64 build instead:

TERRAFORM_VERSION=$(curl -s https://checkpoint-api.hashicorp.com/v1/check/terraform | grep -oP '"current_version":"\K[^"]+')
curl -fsSL "https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_arm64.zip" -o terraform.zip
unzip terraform.zip
sudo mv terraform /usr/local/bin/
terraform -version

Alternative: Install with tfenv (Version Manager)

#

tfenv lets you install and switch between multiple Terraform versions — useful when working on projects that require different versions:

git clone --depth=1 https://github.com/tfutils/tfenv.git ~/.tfenv
echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
 
# Install latest Terraform
tfenv install latest
tfenv use latest
terraform -version

Your First Terraform Project

#

Now that Terraform is installed, create your first configuration:

mkdir ~/terraform-demo && cd ~/terraform-demo

Create a file called main.tf:

terraform {
  required_providers {
    local = {
      source  = "hashicorp/local"
      version = "~> 2.0"
    }
  }
}
 
resource "local_file" "hello" {
  content  = "Hello from Terraform on Amazon Linux 2023!"
  filename = "${path.module}/hello.txt"
}

Run:

terraform init
terraform plan
terraform apply -auto-approve
cat hello.txt

You should see: Hello from Terraform on Amazon Linux 2023!

Upgrade Terraform

#

To upgrade Terraform to the latest version in the future:

sudo dnf upgrade terraform

Uninstall Terraform

#

If you need to remove Terraform:

sudo dnf remove terraform

Automate Terraform Install on EC2 (User Data)

#

To install Terraform automatically when an EC2 instance launches, pass a user-data script. This is ideal for CI runners and bastion hosts:

#!/bin/bash
# EC2 user-data for Amazon Linux 2023
dnf install -y dnf-plugins-core
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
dnf install -y terraform git
terraform version

Provisioning that same runner with Terraform:

resource "aws_instance" "ci_runner" {
  ami           = data.aws_ami.al2023.id
  instance_type = "t3.medium"
 
  user_data = <<-EOF
    #!/bin/bash
    dnf install -y dnf-plugins-core
    dnf config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
    dnf install -y terraform git
  EOF
 
  iam_instance_profile = aws_iam_instance_profile.ci.name
}

Amazon Linux 2 (Legacy)

#

On the older Amazon Linux 2, use yum with the same HashiCorp repository:

sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform

To pin a specific version on Amazon Linux 2:

yum list terraform --showduplicates | sort -r | head -10
sudo yum install terraform-1.8.5-1

Troubleshooting

#

Command not found

#

If terraform is not found after installation:

# Check if it's in your PATH
which terraform
 
# If installed via binary, ensure /usr/local/bin is in PATH
echo 'export PATH="/usr/local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

Permission denied

#
# Ensure the binary is executable
sudo chmod +x /usr/local/bin/terraform

GPG key errors (dnf)

#
# Re-add the repository and refresh keys

Next Steps

#

Now that Terraform is installed on Amazon Linux 2023, continue your learning journey:

Conclusion

#

You've successfully installed Terraform on Amazon Linux 2023. With Terraform, you can now define, provision, and manage infrastructure across AWS, Azure, GCP, and 3,000+ providers using declarative configuration files.

Install Terraform on other Linux distributions:

Related: How to install AWS CLI on macOS using Homebrew — set up AWS CLI in minutes.

#Terraform#Linux#Installation

Share this article