HostRooster Private Cloud and HashiCorp Terraform – Part 1

HashiCorp’s products are often discussed in DevOps and IaaC. Terraform streamlines on-premises and cloud infrastructure provisioning. Terraform manages HostRooster ‘s Public Cloud and GitHub offered a guide. This post discusses HostRooster  Private Cloud and Terraform.

Terraform automates resource development and management for Private Cloud’s VMware vSphere infrastructure. You may use Terraform to deploy and customize VMs from templates. We’ll adapt this example in a second article.

Installation

Terraform is a simple binary on HashiCorp’s website for most OSs. Download and copy it to your PATH directory. Run terraform to test everything.

 

Files and folders

Infrastructure-as-Code tool Terraform uses essential configuration files. Create main.tf. We’ll start with one .tf file because Terraform reads all .tf files by default. We’ll file data in a later piece.

We’ll use files to make Terraform operations easy to understand. Names, passwords, and resources (vCenter, cluster, etc.). Production Terraform use is discouraged. The second post improves code.

Providers

The providers enable you to set Terraform’s external communication. In our case, the vSphere provider will connect to vCenter. Declaring a provider:

 

Terraform uses its data structure (you can also express everything in JSON to automate file generation). Data is arranged in blocks (here, a provider block named vsphere) and stored as keys/values.

Data

Now that Terraform can connect to vCenter, we require vSphere information. We need to know the data center, cluster, template, etc., where we’ll create a virtual machine. Data-type blocks will be used.

 

In the example above, we’re seeking to acquire information about pcc-XXX-XXX-XXX datacenter3113 from datastore pcc-001234 and template UBUNTU. We utilize the data center id to access an object’s information.

Resources

The resources will generate and/or manage infrastructure parts. In our example, we’ll use a virtual_machine resource, which creates VMs.

 

Multiple sub-blocks make this resource complex. First, we’ll name the VM. We detail configuration (Resource pool, datastore, etc.). Virtual devices are configured via the network_interface and disk blocks. The clone sub-block allows you to select a template and set the VM’s OS – OS-specific. Levels use data blocks.

Full example

 

3…2…1… Ignite

Let’s use our new Terraform config file…

Initialization

We can use our configuration file to create a virtual machine now. Let’s start by initiating the terraform environment. This downloads vSphere and creates Terraform’s files.

 

Plan

Next, run terraform plan to ensure our configuration file is error-free and to visualize Terraform’s actions.

 

Before proceeding, examine all plan command output. Errors in the configuration file could remove virtual machines in production. Terraform creates a new resource (a VM) in the scenario below without modifying or deleting anything.

Apply

The terraform apply command configures the infrastructure based on the configuration file. First, Terraform executes the plan command and asks for confirmation by typing yes.

 

Your Private Cloud’s vCenter should show a new virtual machine.

Then

After seeing a Terraform workflow, you may want to test configuration file changes. You can add a virtual drive to your VM by altering its resource block.

 

Run terraform plan to examine what Terraform will do to reconcile infrastructure state with the configuration file.

 

If you agree with, terraform action’s proposal, restart terraform and apply to add a new virtual disk.

Clean up

Run terraform destroy to eliminate all resources when you’re done testing and no longer need the infrastructure. You can’t recover your data after using this command.

 

We’ve shown how to install a virtual machine with Terraform. This taught us plan, apply, and destroy, plus provider, data, and resource. Next, we’ll make this sample more customizable and generic.

HostRooster is a leading web hosting solutions company. Since our founding in 2019, HostRooster has continually innovated new ways to deliver on our mission: to empower people to fully harness the web. Based in London, England, we provide comprehensive tools to users throughout the world so anyone, novice or pro, can get on the web and thrive with our web hosting packages.

%d bloggers like this: