Install Terraform and Gaia Web UI on Ubuntu Server 22.04 – The New Stack

Terraform is an open-source infrastructure-as-code (IaC) tool, created by HashiCorp, that allows users to define and provision data center infrastructure with HashiCorp’s declarative configuration language (called HashiCorp Configuration Langauge) or JSON.

With Terraform, you can define cloud and on-premises resources, using human-readable configuration files that can be versioned, reused, and shared, to create a consistent workflow for provisioning and managing your entire infrastructure. Terraform can be used to manage compute, storage, network resources, DNS entries, and SaaS features.

Let’s install Terraform on my Ubuntu open source server of choice, then see how to provision an AWS EC2 instance.

Install Terraform

The first thing we need to do is install Terraform. Before doing that, let’s install some necessary dependencies. Log in to your Ubuntu instance and install these requirements with the command:

sudo apt-get install  software-properties-common gnupg2 curl -y

Once these dependencies are taken care of, download the required HashiCorp GPG key with the command:

curl https://apt.releases.hashicorp.com/gpg | gpg --dearmor > hashicorp.gpg

Next, add the GPG key with:

sudo install -o root -g root -m 644 hashicorp.gpg /etc/apt/trusted.gpg.d/

Now we can add the official HashiCorp repository with the command:

sudo apt-add-repository "deb [arch=$(dpkg --print-architecture)] https://apt.releases.hashicorp.com focal main"

Finally, update apt and install Terraform with the following two commands:

sudo apt update

sudo apt install terraform

Once the installation is complete, you can verify the installation with the command:

terraform version

The output should look like this:

Terraform v1.2.4

on linux_amd64

+ provider registry.terraform.io/hashicorp/google v4.27.0

So far, so good. Go further.

How to provision an AWS EC2 instance with Terraform

For this to work, you will need both your access key and your secret key for Amazon Web Services. These credentials are located in the Security Credentials section of your AWS Web Services console. You will then need to generate a new access key, which (when created) will show the access key ID and secret access key, both of which are random strings of characters. You will need to copy these two keys, because we are going to use them.

On your Ubuntu instance, create a new demo directory with the command:

mkdir terraform_test

Create a new Terraform configuration file with the command:

nano awsec2.tf

In this file, paste the following:

Where ACCESS_KEY is the AWS access key you created, SECRET_KEY is the associated secret key, and AMI is the friend of the Ubuntu version you want to use. For example, the friend for Ubuntu 22.04 in zone us-west-1 is friend-09dadf5dc6cfa5248.

Save and close the file.

We can now initialize terraform with the command:

terraform init

Once the initialization is complete, you can move on to the planning stage, which will create an execution graph for building and provisioning our infrastructure. The command for this is:

terraform plan

The above command shouldn’t take too long. Once it completes, you can then move on to the apply stage, which runs the configuration file to launch our AWS EC2 instance. To do this run the command:

terraform apply

You will be asked to verify that you want to perform the actions, so type yes and press Enter on your keyboard. The instance will be deployed and should be listed in your AWS Dashboard.

What about a Terraform GUI?

There are a few Terraform GUIs available, but many of them are either incredibly difficult to implement or are just plain broken. There is one, however, that can be deployed with Docker Compose, called Gaia. Let’s deploy it.

The first thing to do is to install Docker CE.

First, add the GPG key with the command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Next, add the Docker repository:

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Install the necessary dependencies with:

sudo apt-get install apt-transport-https ca-certificates curl gnupg lsb-release -y

Finally, we can install the latest version of the Docker engine:

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io -y

Finally, add your user to the docker group with the command:

sudo usermod -aG docker $USER

Notify the system of changes with:

newgrp docker

Create a Docker Compose YAML with:

nano docker-compose.yml

In this file, paste the following:

Save and close the file.

Deploy the Gaia container with:

docker-compose up -d

Once the container is up and running, access the web UI by pointing a web browser to http://SERVER:8080 (where SERVER is the IP address or domain of the hosting server).

You should be greeted by the Gaia login screen (Figure 1), where the credentials are admin/admin123).

Figure 1: The Gaia login screen.

Upon successful login, you will be taken to the main Gaia window (Figure 2), where you can start working with Terraform.

    Gaia's web UI makes working with Terraform much easier.

Figure 2: Gaia’s web UI makes working with Terraform much easier.

Congratulations, you now have a much easier tool to manage Terraform that will help you provision your data center infrastructure for cloud native development. Take advantage of this new power.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: Docker.

Comments are closed.