Terraform is a handy tool for quick provisioning and managing cloud infrastructure using configuration files.
When the configuration files change, Terraform automatically re-reads them and proceeds to create, update, or delete the resources, as described by the administrator.
In this article, we will describe how to begin working with Terraform and create cloud infrastructure in Hostman using it.
You can find more details on using Hostman Terraform provider in our GitHub profile.
First, let’s learn how to install Terraform on Linux, Windows, and macOS.
You can run these commands as root
or as a user with sudo
privileges.
First, update the package list:
apt update
Next, install wget
and unzip
we’ll use later, navigate to the directory where Terraform will be installed, and download the package. Run these commands consecutively:
apt install wget unzip
cd ~
wget https://releases.hashicorp.com/terraform/1.12.2/terraform_1.12.2_linux_amd64.zip
In the last command, we specify the latest version as of writing—1.12.2. You can check the latest releases here.
Now, unzip the archive:
unzip terraform_1.12.2_linux_amd64.zip
And move the file to the directory:
mv terraform /usr/local/bin/
That’s it. Now you can verify that Terraform is successfully installed:
terraform -v
If everything works correctly, you will see a similar output:
Terraform v1.12.2
on linux_amd64
There are two ways of installing Terraform on Windows: via the Chocolatey package manager or manually.
While the Chocolatey method is straightforward, it won’t allow you to install the latest software version. If you want the latest version of Terraform, use the manual installation.
The easiest way to install Terraform on Windows is using Chocolatey.
If you don’t have it yet, open PowerShell as administrator and run:
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Now you can use Chocolatey to install software on your machine.
Enter the following command into PowerShell window to install Terraform:
choco install terraform
Verify that Terraform is successfully installed:
terraform -v
To install the latest version of Terraform, follow these steps:
C:\Terraform
.At this point, Terraform is installed; however, you can only use it when specifying the full path in the command line:
C:\Windows\system32>C:\Terraform\terraform -v
Terraform v1.12.2
on windows_amd64
If you try to access Terraform by simply using its name, you will get an error:
C:\Windows\system32>terraform -v
'terraform' is not recognized as an internal or external command,
operable program or batch file.
To be able to call terraform from the command line, you need to add it to the PATH
environment variable.
Check that everything works well by running in the command line:
terraform -v
Output:
Terraform v1.12.2
on windows_amd64
As with Windows, there are two ways of installing Terraform on your macOS machine: via the Homebrew package manager or by downloading the binary.
First, you need to install the official Hashicorp repository. Open the terminal and run:
brew tap hashicorp/tap
Next, you can install Terraform by running:
brew install hashicorp/tap/terraform
Check that the installation was successful by requesting the Terraform version:
terraform -v
PATH
using the command:echo $PATH
Now, you need to place the Terraform executable file to one of the locations in the list. You can do it with the command:
mv ~/current/terraform/location /one/of/PATH/locations/
For example, if the executable is located in your Downloads and your PATH includes /usr/local/bin
, the command will look like this:
mv ~/Downloads/terraform /usr/local/bin/
Terraform configuration files have the .tf
extension. The file name is arbitrary; the program can read it anyway.
Let’s create a new directory (hostman_project
) and place a configuration file there. We’ll call it hm_project.tf
.
To create a .tf
file, you can first make a simple text file and then change its extension to .tf
.
One directory can have several configuration files belonging to the same project. For example, you can create a vars.tf
file for describing variables, nginx.tf
for configuring an Nginx server, etc. When you run terraform plan, all the .tf
files will be combined into one.
However, in this article, we’ll be using one configuration file, hm_project.tf
.
Now let’s configure the Terraform provider in our configuration file.
At the beginning of the hm_project.tf
file add the following lines:
terraform {
required_providers {
hm = {
source = "hostman-cloud/hostman"
}
}
required_version = ">= 0.13"
}
In the source
field, enter the provider URL. In this case, Hostman’s.
Now navigate to the directory with the configuration file and run this command to install the provider:
terraform init
To work with the provider, you will need an API token that you can obtain in the API section of your control panel.
For example, we have obtained the token fb246030216d5g30b1g6228e3071037g
.
We need to add it to the hm_project.tf
file like this:
provider "hm" {
token = "fb246030216d5g30b1g6228e3071037g"
}
Make sure to replace fb246030216d5g30b1g6228e3071037g
with your own token.
Using Terraform, you can manage various resources in your Hostman cloud, such as cloud servers, cloud databases, VPC, public IP addresses, etc.
As an example, let’s create a cloud server with the following specs:
We’ll name the server my-hostman-server
; you can set up your own name.
To create the server, add the following configuration into the hm_project.tf
file:
data "hm_configurator" "configurator" {
location = "us-2"
}
data "hm_os" "os" {
name = "ubuntu"
version = "22.04"
}
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
configuration {
configurator_id = data.hm_configurator.configurator.id
disk = 1024 * 80
cpu = 2
ram = 1024 * 4
}
}
If the program failed to create the server, check if you have entered the token correctly.
Pay attention to the errors that Terraform displays; in most cases, they make it clear what went wrong.
Additionally, you can set up SSH key authentication. You can generate the keys using this guide in our documentation. Next, copy your public key to the server by adding the following configuration to the hm_project.tf
file:
resource "hm_ssh_key" "your-key" {
name = "Your key"
body = file("~/.ssh/your-key.pub")
}
In the resource
block, add the line ssh_keys_ids
:
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
ssh_keys_ids = [hm_ssh_key.your-key.id]
}
Our final configuration file should look like this:
terraform {
required_providers {
hm = {
source = "hostman-cloud/hostman"
}
}
required_version = ">= 0.13"
}
provider "hm" {
token = "fb246030216d5g30b1g6228e3071037g"
}
data "hm_configurator" "configurator" {
location = "us-2"
}
data "hm_os" "os" {
name = "ubuntu"
version = "20.04"
}
resource "hm_ssh_key" "your-key" {
name = "Your key"
body = file("~/.ssh/your-key.pub")
}
resource "hm_server" "my-hostman-server" {
name = "My Hostman Server"
os_id = data.hm_os.os.id
ssh_keys_ids = [hm_ssh_key.your-key.id]
configuration {
configurator_id = data.hm_configurator.configurator.id
disk = 1024 * 80
cpu = 2
ram = 1024 * 4
}
}
Use this command:
terraform validate
If all the settings are correct, you will see the message that the configuration is valid. If any errors occur, check your configuration again.
First, enter the following command:
terraform plan
It doesn’t apply your configuration, but displays all the planned resources so you can check the settings. If there are any issues, Terraform will point you to the places in the configuration that are set incorrectly.
If and when everything is fine, run:
terraform apply
This command applies the configuration and allows Terraform to create the described resources. Confirm by typing yes
and pressing Enter.
Now you can visit your control panel and make sure the resources are created.
Should you decide to remove them, you can use the command:
terraform destroy
In the next step, confirm the deletion by typing yes
.
In this guide, we described how to create Terraform configuration files and create and configure resources in the Hostman cloud.
To learn more about working with Terraform, refer to the official documentation. And to find more about the Hostman Terraform provider, check our GitHub page.