Deploy Psychz Dedicated Server Using Terraform Provider
Publisher: Psychz Networks, June 21,2023- Introduction
- Prequisites
- Install Terraform
- Configure Provider Environment
- Building Provider
- Provider Installation
- Initializing Terraform
- Order Plans
- Order Express (Deploy Server)
Introduction
Terraform is an open-source infrastructure-as-code (IaC) tool that allows you to define and provision infrastructure resources across various cloud platforms. Whether you are working with Ubuntu or Alma Linux, this article will guide you through the process of getting started with Terraform.
Prerequisites
Before you begin, ensure that you have the following prerequisites in place:
- Debian/Ubuntu or RHEL/CentOS/Alma Linux installed on your machine.
- A text editor of your choice (e.g., Vim, Nano, Visual Studio Code).
- Terraform 1.8.5
- Go 1.22.4
Install Terraform
You can install the latest version of Terraform on most operating systems from the command line using various package managers.
Debian/Ubuntu
To install Terraform on Ubuntu, add the HashiCorp GPG key to your system:
# curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
Next, add the official HashiCorp Terraform Linux repository to apt:
# apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Then update apt and install Terraform:
# apt-get update && sudo apt-get install terraform
Once installed, verify the installation:
# terraform -v
The command returns Terraform’s version information:
Terraform v1.8.5
CentOS/RHEL/AlmaLinux
# yum install -y yum-utils
# yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
# yum -y install terraform
Configure Provider Environment
The Provider is responsible for understanding and managing the lifecycle of a specific type of infrastructure resource. It interfaces between Terraform and your desired infrastructure or service at Psychz. To configure the Provider environment, we will install Go
Installing GoLang
To get the latest version of Go, visit to the official Go downloads page in your web browser. From there, copy the URL for the current binary release’s tarball.
As of this writing, the latest release is go1.22.4. To install Go on an Ubuntu server (or any Linux server, for that matter), copy the URL of the file ending with linux-amd64.tar.gz.
IMPORTANT: Please confirm that you’re in the home directory before you initiate the installation
Use the following command to retrieve the tarball
# curl -OL https://golang.org/dl/go1.22.4.linux-amd64.tar.gz
Now, extract the tar file using the following commmand into the following directory /usr/local/
# tar -C /usr/local -xvf go1.22.4.linux-amd64.tar.gz
Flags:
-C: instructs tar to change to the given directory
x: Extract tar file
v: Lists files being extracted
f: Tell we'll specify filename
Setting Go path
Add /usr/local/go/bin to the PATH environment variable. You can do this by adding the following line to your $HOME/.profile or /etc/profile (for a system-wide installation):
# nano ~/.profile
Then, add the following information to the end of your file:
export PATH=$PATH:/usr/local/go/bin
refresh your profile by running the following command
# source ~/.profile
To check the installed version of Go, use the following command
# go version
Output
Building the Provider
We will now setup Psychz Terraform Provider repository on our system. We will copy/clone the repository from GIT into $HOME/development directory. Follow the steps given below to complete the setup
Cloning Psychz Terraform Provider repository
# mkdir -p $HOME/development; cd $HOME/development
# git clone https://github.com/psychz-networks/terraform-provider-psychz.git
Now let us build the Provider. To do so, you must be inside the cloned Psychz Terraform provider directory.
# cd terraform-provider-psychz
# go build
Provider Installation
IMPORTANT: Manual provider installation is needed only for manual testing of custom-built Psychz provider plugin.
Manual installation process differs depending on Terraform version. Run terraform version command to determine version of your Terraform installation.
Create psychz.net/psychz/psychz/1.0.1/linux_amd64 directories under
# mkdir -p ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.1/linux_amd64
Copy Psychz provider binary file there.
# cp terraform-provider-psychz ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.1/linux_amd64
Shell Configuration to store Access Tokens
While this step is optional, it is strongly recommended to enable it as an additional security measure. This will help conceal the username and token, preventing them from being exposed frequently. This method permits the secure storage of keys in the system’s configuration files, allowing for real-time loading.To make the variables permanent, you can add the export command to your shell's configuration file ('~/. bashro, ~/. bash profile, '~/.zshrc,etc.).
Open your shell configuration file in a text editor. For example, if you are using bash, you can use:
# nano ~/.bashrc
Add the export command to the end of the file:
# export PSYCHZ_ACCESS_TOKEN=myAccessToken
# export PSYCHZ_ACCESS_USERNAME=myUsername
Save the file and exit the editor. To apply the changes, source the file:
# source ~/.bashrc
Initializing Terraform
We will now initialize Terraform by running the "terraform init" command. This command prepares Terraform project by downloading the necessary provider plugins and setting up the working directory. It ensures that Terraform has all the required dependencies to manage your infrastructure and allows you to begin creating and managing resources.
IMPORTANT: Before you execute the init command, ensure that you are in the desired folder. E.g. order_details, order_express, order_plan, services_details
# terraform init
OUTPUT
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.
IMPORTANT: If you encounter version related issue while initiating Terraform, please follow manual provider installation steps as given below
Terraform 1.4.6 and newer
Create psychz.net/psychz/psychz/1.0.1/linux_amd64 directories under:
Note: adjust linux_amd64 from above structure to match your os_arch
# mkdir -p ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.1/linux_amd64
Copy Psychz provider binary file there.
# cp terraform-provider-psychz ~/.terraform.d/plugins/psychz.net/psychz/psychz/1.0.1/linux_amd64
In every Terraform template directory that uses Psychz provider, ship below terraform.tf file (in addition to other Terraform files)
terraform {
required_providers {
psychz = {
source = "psychz.net/psychz/psychz"
version = "1.0.1" } } }
Done! Local Psychz provider plugin will be used after terraform init command execution in Terraform template directory
Order Plan
Order Plan gives you all the details about the different types of hardware you can choose from. You get the nitty-gritty technical stuff, like specs and pricing, and even where they're located. This makes it super easy for you to pick and set up the perfect machine whenever you need it, just by using Psychz Terraform Provider and plugging in the info you want.
Note: Before you execute the init command, ensure that you are in the folder ~/order_plan
# terraform init
# terraform apply
Input details
var.billing_type
var.category_id
var.location_code
var.option_detail
var.plan_id
provider.psychz.access_token
provider.psychz.access_username
IMPORTANT: If you have configured Access_Token and Access_Username in your Bash Profile, then system will skip the input steps.
Sample output
data.psychz_order_plans.server: Read complete after 1s [id=psychz_order_plans]
Changes to Outputs:
+ server_order_plan = jsonencode(
{
+ data = {
+ dedicated_servers = {
+ "138" = {
+ base_price = "34.00"
+ billing_type = "1"
+ category_id = 2
+ category_name = "Dedicated Servers"
+ location_name = "Los Angeles, USA"
+ plan_id = "138"
+ plan_name = "Los Angeles :: E3-1230 v2"
+ reseller_price = "28.9"
+ standard_note = ""
}
The output provides a big list of all the hardware options with their specs, neatly organized in JSON format. You can save this info on your computer and check it out whenever you want. It's like having a handy reference guide for later!
IMPORTANT: You will also need Access Token and Access Username to successfully perform the above action. Refer to the following article How to setup API Access to know how to gain information on Token and Username.
Order Express
We will now run Terraform to input the changes. The "terraform apply" command executes the changes specified in the Terraform configuration file and apply them to the infrastructure. It provisions or modifies resources based on the desired state and updates the infrastructure accordin
Note: You must be inside the ~/order_express folder before you execute the following commands
# terraform init
# terraform apply
The system will now request you to input information against the following fields (in the same sequence)
+ client_id
+ disk_partition_id
+ enforce_password_change
+ hostname
+ id
+ message
+ order_quantity
+ os_cat
+ os_id
+ partner_id
+ password
+ payment_mode
+ plan_id
+ private_key
+ software_raid
Once you have successfully entered all the required details, the system will ask you for final approval before executing the code. You need to type "yes" to proceed further.
Sample output
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.
Enter a value: yes
After entering "yes, " Terraform will connect with our API server using your provided credentials and execute the order. This typically takes about a minute or less. Once done, the system will post the following output
psychz_order_express.express_server: Still creating... [10s elapsed]
psychz_order_express.express_server: Still creating... [20s elapsed]
psychz_order_express.express_server: Still creating... [30s elapsed]
psychz_order_express.express_server: Still creating... [40s elapsed]
psychz_order_express.express_server: Still creating... [50s elapsed]
psychz_order_express.express_server: Creation complete after 53s [id=858145]
data.psychz_order_detail.server: Reading...
data.psychz_order_detail.server: Read complete after 1s [id=858145]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
After a successful execution, the output displays the details of the orders and its attributes that you have inputted to commission the server.
Congratulations! If you've made it to the end of this article without encountering any errors or issues, you have successfully installed the Psychz Terraform provider and deployed a server using it. You're now equipped with the knowledge to leverage Terraform and Psychz to efficiently manage and provision your infrastructure. Happy building and automating your infrastructure with ease!