Terraform – Install on CentOS 8

This tutorial shows how to install Terraform on the Linux machine (CentOS 8). CentOS 8 uses DNF (Dandified yum) as a package manager.

Check OS

cat /etc/redhat-release

Switch User to super user

sudo su

Update the System

dnf check-update

dnf upgrade -y

Add Hashicorp Repository

dnf config-manager --add-repo=https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
  
dnf repolist

Install Terraform

dnf install terraform -y

exit

Verify the Installation

terraform -help

Enable Tab Completion

touch ~/.bashrc
terraform -install-autocomplete

Tutorial

Launch the nginx docker image using Terraform. You need to install Docker.

Write the Terraform script

mkdir terraform-test

cd terraform-test

vi main.tf
terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.23.1"
    }
  }
}

provider "docker" {}

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

resource "docker_container" "nginx" {
  image = docker_image.nginx.image_id
  name  = "test"
  ports {
    internal = 80
    external = 8080
  }
}

Initialize and Provision

terraform init

terraform apply

docker container ls

Open the web browser and access http://{host-ip}:8080. You can see the nginx home page.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s