DevOps

Install Terraform and the Gaia Web UI on Ubuntu Server 22.04

2 Jul 2022 6:00am, by

Terraform is an open source Infrastructure as Code (IaC) tool, created by HashiCorp, that allows users to define and provide data center infrastructure with either HashiCorp’s declarative configuration language (known as HashiCorp Configuration Langauge) or JSON.

With Terraform you can define both 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 all of your infrastructure. Terraform can be used to manage compute, storage, networking resources, DNS entries, and SaaS features.

Let’s get Terraform installed on my go-to open source server of choice Ubuntu and then see how to provision an AWS EC2 instance.

Installing Terraform

The first thing we must do is install Terraform. Before we do this, let’s install a few necessary dependencies. Log in to your Ubuntu instance and install those requirements with the command:

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

Once those 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 something like this:

Terraform v1.2.4

on linux_amd64

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

So far so good. Let’s take this one step further.

How to Provision an AWS EC2 Instance with Terraform

In order to make this work, you’re going to need both your access key and your secret key for  Amazon Web Services. These credentials are found in the Security Credentials section of your AWS Web Services console. You will then have to generate a new access key, which (once created) will display the Access Key ID and the Secret Access Key, which are both strings of random characters. You’ll need to copy those two keys down, as we’re 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 that 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 ami of the Ubuntu version you want to use. For example, the ami for Ubuntu 22.04 in the us-west-1 azone is ami-09dadf5dc6cfa5248.

Save and close the file.

We can now initialize terraform with the command:

terraform init

After the initialization completes, you can move onto the plan stage, which will create an execution graph for the creation and provisioning of 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 executes the configuration file to launch our AWS EC2 instance. For this, issue the command:

terraform apply

You will be asked to verify that you want to perform the actions, so type yes and hit 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 challenging to get up and running or they simply are broken. There is one, however, that can be deployed with Docker Compose, called Gaia. Let’s get that deployed.

The first thing we must do is 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

To finish, add your user to the docker group with the command:

sudo usermod -aG docker $USER

Make the system aware of the changes with:

newgrp docker

Create a Docker Compose YAML with:

nano docker-compose.yml

In that 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.

Figure 1: The Gaia login screen.

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

 The Gaia web UI makes working with Terraform considerably easier.

Figure 2: The Gaia web UI makes working with Terraform considerably easier.

Congratulations, you now have a much easier tool for managing Terraform, which will help you provision your data center infrastructure for cloud native development. Enjoy that newfound power.

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