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. 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 v0.13.5

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
  }
}

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 v2.7.2...
- Installed terraform-providers/docker v2.7.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 = "~> 2.7.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.

More guides:

 Install PostgreSQL 13 on Amazon Linux 2

Grep vs Awk vs Sed Commands in Linux

How To Install and Use Websploit on Ubuntu

LEAVE A REPLY

Please enter your comment!
Please enter your name here

eighteen + 9 =