Terraform – Install on Amazon Linux 2

This tutorial shows how to install Terraform on the Linux machine (Amazon Linux 2).

Check OS

cat /etc/os-release

Switch User to super user

sudo su

Update the System

yum update -y

yum install -y yum-utils

Add Hashicorp Repository

sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
  
yum repolist

Install Terraform

yum 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 )

Twitter picture

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

Facebook photo

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

Connecting to %s