Terraform is an open-source Infrastructure as Code (IaC) tool designed by HashiCorp. It allows users to define and provision infrastructure in a declarative manner using configuration files. By supporting a variety of cloud providers and on-premises systems, Terraform streamlines the process of managing and automating infrastructure, ensuring consistent and reproducible environments.


This article will guide you through using Terraform with Genesis Cloud to set up your infrastructure.


Prerequisites

  1. Genesis Cloud account

    1. Create an account here, if you do not have it

    2. If you have an account, then login to it here

  2. Genesis Cloud API Token

    1. Generate an API Token here

    2. API Documentation


Step 1 - Install Terraform


Step 2 - Configure Terraform for Genesis Cloud


  • Terraform offers compatibility with a wide range of service providers. Each provider has its own specifications, which generally map to the API of its respective service provider.

  • The Genesis Cloud provider lets Terraform interact with the Genesis Cloud API to build out infrastructure. This provider supports creating the following Genesis Cloud resources.

    • genesiscloud_instance

    • genesiscloud_security_group

    • genesiscloud_snapshot

    • genesiscloud_ssh_key

    • genesiscloud_volume

    • genesiscloud_filesystem

    • genesiscloud_floating_ip

  • Once you have downloaded and installed Terraform on your local system, you'll need to configure it for Genesis Cloud.

  • Before proceeding, verify the Terraform version by running the command “terraform -v“ in your local OS terminal.

  • Next, create a new directory for your Terraform project and navigate into it. In this directory, create a Terraform configuration file with a .tf extension (e.g., main.tf). You can use a simple text editor for this purpose.

  • Open the main.tf file and add the below ‘Terraform Configuration’ in that file.

terraform {

  required_providers {

    genesiscloud = {

      source = "genesiscloud/genesiscloud"

      # version = "..."

    }

  }

}


provider "genesiscloud" {

  # optional configuration...


  # set GENESISCLOUD_TOKEN env var or:

  # token = "..."

}


  • Uncomment the ‘version’ attribute from the configuration and set the version (e.g., 1.1.4). Uncomment the ‘token’ attribute, and add your ‘Genesis Cloud API Token’. Then, ‘save’ the file.

  • Open a terminal in the directory of your Terraform project and run “terraform init“. This command initializes a Terraform configuration, downloading necessary plugins, modules, and dependencies. It prepares your environment for Terraform to manage your infrastructure effectively.


Step 3 - Create a Resource


Create an SSH Key

  • You can add the configuration for the SSH_Key in the main.tf file or you can create a separate file with .tf extension within that directory to add SSH_Key configuration.

  • Just add the below code snippet in that file

resource "genesiscloud_ssh_key" "example" {

  name       = "example"

  public_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBOpdKM8wSI07+PO4xLDL7zW/kNWGbdFXeHyBU1TRlBn alice@example.com"

}

  • Give any name to your ssh_key and replace ‘public_key’ with your actual ‘SSH_Public_Key’. ‘Save’ the file.

  • Then, run 'terraform plan'. This command shows you what changes Terraform will make without actually applying them.

  • Next, run ‘terraform apply' and confirm by typing 'yes’. Terraform will then apply your configuration and create the specified resource.


Create an Instance

  • Similarly, you can add the configuration for the instance in the main.tf file or create a separate file with a .tf extension within that directory for instance configuration.

  • Just add the below code snippet in that file.

# Create an instance:


locals {

  region = "ARC-IS-HAF-1"

}


resource "genesiscloud_instance" "instance1" {

  name   = "instance1"

  region = local.region


  image = "ubuntu-22.04"

  type  = "vcpu-4_memory-12g_disk-80g_nvidia3080-1"


  ssh_key_ids = [

    genesiscloud_ssh_key.example.id,

  ]

 disk_size = 80

}


output "connect" {

  value = "ssh ubuntu@${genesiscloud_instance.instance1.public_ip}"

}


  • Give any name to your instance and and add the type of the instance according to your preferences. You can check out our Instance types here: Instance Types

  • Then, run 'terraform plan' to see what changes Terraform will make without actually applying them and run ‘terraform apply' & confirm by typing 'yes’ to apply your configuration and create the specified infrastructure.

  • Now, you have created the resource.


Note: We recommend you to refer our Terraform Documentation for more information.

Example (Let’s create Instance & Volume)


First, check your Terraform installation by running 'terraform -v' in the terminal of your local OS.Output screen of 'terraform -v' command

                                                               Output screen of 'terraform -v' command


Configuring Terraform for Genesis Cloud

  • Adding 'Terraform Configuration' in main.tf fileShows 'Terraform Configuration' in main.tf file                                             Shows 'Terraform Configuration' in main.tf file

  • Running 'terraform init'Output screen of 'terraform init' command                                                         Output screen of 'terraform init' command


Creating SSH_Key

  • Adding configuration for the ‘SSH_Key’ in main.tf fileShows configuration for the ‘SSH_Key’ in main.tf file                        Shows configuration for the ‘SSH_Key’ in main.tf file

  • Running 'terraform plan'Output screen of 'terraform plan' command for SSH_Key                                                Output screen of 'terraform plan' command for SSH_Key

  • Running 'terraform apply'Output screen of 'terraform apply' command for SSH_Key                                           Output screen of 'terraform apply' command for SSH_Key


Creating Instance

  • Adding configuration for the 'Instance' in main.tf fileShows configuration for the 'Instance' in main.tf file                                                  Shows configuration for the 'Instance' in main.tf file

  • Running 'terraform plan'Output screen of 'terraform plan' command for Instance                                                Output screen of 'terraform plan' command for Instance

  • Running 'terraform apply'Output screen of 'terraform apply' command for Instance                                               Output screen of 'terraform apply' command for Instance


Creating Volume

  • Adding configuration for the 'Volume' in main.tf fileShows configuration for the 'Volume' in main.tf file                          Shows configuration for the 'Volume' in main.tf file

  • Running 'terraform plan'Output screen of 'terraform plan' command for Volume                                            Output screen of 'terraform plan' command for Volume

  • Running 'terraform apply'Output screen of 'terraform apply' command for Volume                                              Output screen of 'terraform apply' command for Volume