How To Install Terraform on Amazon Linux 2

Terraform is an Infrastructure as Code(IaC) tool created to help users predictably create, change, and improve infrastructure. This tool codifies infrastructure management in declarative way, easy to deploy, manage and destroy. Terraform uses providers to manage external resources by invoking endpoint’s APIs. In this short guide we will install and use Terraform automation tool on Amazon Linux 2.

Install Terraform on Amazon Linux 2

Terraform binary is provided by Hashicorp for all Linux distributions for easy installation. You can either install terraform from official repos using your package manager or install it manually using terraform binary.

Using package manager

To isntall using your package manager, run the following commands:

#Install yum-config-manager to manage your repositories.
sudo yum install -y yum-utils

#Use yum-config-manager to add the official HashiCorp Linux repository.
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo

#Install Terraform from the new repository.
sudo yum -y install terraform

#Confirm installation
$ terraform version
Terraform v1.9.8
on linux_amd64

Manual Installation using binary

All you have to do is download the latest binary archive, extract and place it in a directory within your PATH.

Get the latest release version.

TERRAFORM_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'`

To download the latest release of Terraform on Amazon Linux 2, run the command:

wget https://releases.hashicorp.com/terraform/${TERRAFORM_VER}/terraform_${TERRAFORM_VER}_linux_amd64.zip

Extract the file.

sudo yum -y install unzip
unzip terraform_${TERRAFORM_VER}_linux_amd64.zip

Move binary file to the /usr/local/bin directory:

sudo mv terraform /usr/local/bin/

Confirm installation by checking the version of Terraform.

$ terraform version
Terraform v1.9.8
on linux_amd64

Enable tab completion:

terraform -install-autocomplete
source ~/.bashrc

Using Terraform on Amazon Linux

Terraform documentation have lots of resources on how you can use Terraform to manage your complete Infrastructure lifecycle.

We can show you a simple terraform usage with Docker. Install Docker CE on Amazon Linux with the following commands.

sudo yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum -y install curl wget unzip awscli aws-cfn-bootstrap nfs-utils chrony conntrack jq ec2-instance-connect socat
sudo amazon-linux-extras enable docker
sudo yum -y install docker
sudo systemctl enable --now docker
sudo usermod -aG docker $USER
newgrp docker

Create temporary project directory:

mkdir terraform-docker-lab
cd terraform-docker-lab
vim main.tf

Create main.tf Terraform configuration file.

terraform {
  required_providers {
    docker = {
      source = "terraform-providers/docker"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}

Now, download the plugin that allows Terraform to interact with Docker.

$ terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of terraform-providers/docker...
- Installing terraform-providers/docker v3.0.2...
- Installed terraform-providers/docker v3.0.2 (signed by HashiCorp)

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* terraform-providers/docker: version = "~> 3.0.2"

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.

Create Nginx docker container with the terraform apply command.

terraform apply

Agree to create resources.

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

A docker container should be created in few seconds.

docker_image.nginx: Creating...
docker_image.nginx: Creation complete after 6s [id=sha256:f35646e83998b844c3f067e5a2cff84cdf0967627031aeda3042d78996b68d35nginx:latest]
docker_container.nginx: Creating...
docker_container.nginx: Creation complete after 0s [id=43511f4c8fdb1c7993ad01d349c693b9f4dd5b19522a8ae1dc25192875ad20ae]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

You can use docker ps to confirm it is running:

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
43511f4c8fdb        f35646e83998        "/docker-entrypoint.…"   36 seconds ago      Up 34 seconds       0.0.0.0:8000->80/tcp   tutorial

If you curl port 8000 on the host you should get Welcome to nginx page.

$ curl localhost:8000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

To destroy the Infrastructure run the command:

terraform destroy

Type yes when asked to initiate destruction of resources:

Plan: 0 to add, 0 to change, 2 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_container.nginx: Destroying... [id=43511f4c8fdb1c7993ad01d349c693b9f4dd5b19522a8ae1dc25192875ad20ae]
docker_container.nginx: Destruction complete after 0s
docker_image.nginx: Destroying... [id=sha256:f35646e83998b844c3f067e5a2cff84cdf0967627031aeda3042d78996b68d35nginx:latest]
docker_image.nginx: Destruction complete after 0s

Destroy complete! Resources: 2 destroyed.

Explore More with CloudSpinx

Looking to streamline your tech stack? At CloudSpinx, we deliver robust solutions tailored to your needs.

Learn more about how we can support your journey with CloudSpinx.

Check out our other articles about Terraform:

Your IT Journey Starts Here!

Ready to level up your IT skills? Our new eLearning platform is coming soon to help you master the latest technologies.

Be the first to know when we launch! Join our waitlist now.

Join our Linux and open source community. Subscribe to our newsletter for tips, tricks, and collaboration opportunities!

Recent Post

Leave a Comment

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

Related Post

In this article, you will learn how to install Nodejs on Rocky Linux / AlmaLinux 8 with NPM. Nodejs is […]

MariaDB is a community version of MySQL database server. The latest stable version of MariaDB is 10.6. In a database […]

Xfce is a lightweight desktop environment for UNIX-like operating systems designed to run fine on minimal system resources ie (small […]

Let's Connect

Unleash the full potential of your business with CloudSpinx. Our expert solutions specialists are standing by to answer your questions and tailor a plan that perfectly aligns with your unique needs.
You will get a response from our solutions specialist within 12 hours
We understand emergencies can be stressful. For immediate assistance, chat with us now

Contact CloudSpinx today!

Download CloudSpinx Profile

Discover the full spectrum of our expertise and services by downloading our detailed Company Profile. Simply enter your first name, last name, and email address.