Terraform is a powerful infrastructure-as-code tool that enables users to provision, manage, and automate cloud infrastructure. By following this tutorial, you will gain a basic understanding of how to use Terraform to create and manage resources on LetsCloud. The guide covers a straightforward method; however, Terraform provides extensive customization options for more advanced infrastructure setups.

Prerequisite

Before you begin, make sure you have Terraform installed on your system. You can download it from the official Terraform website and follow the instructions for your operating system (Windows, Linux, or macOS).

Goals

  • Learn how to quickly set up a basic instance using Terraform on LetsCloud.
  • Understand the fundamental steps for infrastructure provisioning using Terraform.

Step 1: Install Terraform

Ensure Terraform is installed by following the Terraform Installation Guide. You can verify the installation with:

$

terraform -version

Expected output:

Terraform v1.8.4

This confirms that Terraform is available in your system PATH and ready to use.

Step 2: Setup Terraform Provider for LetsCloud

Create a dedicated project directory and navigate into it:

$$

mkdir letscloud-terraform cd letscloud-terraform

Inside this directory, create a new file named main.tf. This file will define your Terraform configuration, including the required provider and your credentials:

main.tf

terraform {
  required_providers {
    letscloud = {
      source = "letscloud-community/letscloud"
    }
  }
}

provider "letscloud" {
  #API token can be set via LETSCLOUD_API_TOKEN environment variable
  #api_token = "your-api-token"
}

The letscloud provider allows Terraform to communicate with the LetsCloud API and manage infrastructure.

Step 3: Create an SSH Key

If you don't already have an SSH key pair, generate one using:

$

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Accept the default path when prompted (usually ~/.ssh/id_rsa).

Then, add the key to your Terraform config:

main.tf

resource "letscloud_ssh_key" "main" {
  label = "main-key"
  key   = file("~/.ssh/id_rsa.pub")
}

This step ensures secure access to your instance after creation.

Step 4: Configure and Create the Instance

Add this to your main.tf to define the instance to be provisioned:

main.tf

resource "letscloud_instance" "basic" {
  label         = "basic-instance"
  plan_slug     = "1vcpu-1gb-10ssd"
  image_slug    = "ubuntu-24.04-x86_64"
  location_slug = "MIA1"
  hostname      = "basic-instance.example.com"
  ssh_keys      = [letscloud_ssh_key.main.id]
  password      = "your-secure-password" # Still required as backup
  depends_on    = [letscloud_ssh_key.main]
}

You can modify plan_slug, image_slug, and location_slug to fit your needs. These are typically available in the provider documentation or via the LetsCloud API.

Step 5: Apply Terraform Configuration

First, initialize your Terraform working directory:

$

terraform init

This downloads the required provider plugins and prepares the environment.

Next, preview the execution plan:

$

terraform plan

Finally, apply the configuration:

$

terraform apply

Confirm the action by typing yes when prompted. Example output:

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Step 6: Access Your Instance

After successful creation, Terraform will output information about the instance, including its public IP.

You can access it with:

$

ssh root@your-instance-ip

Make sure your private key (~/.ssh/id_rsa) has the correct permissions:

$

chmod 600 ~/.ssh/id_rsa

Conclusion

Congratulations! You've successfully provisioned a LetsCloud instance using Terraform. This automated approach enhances efficiency and repeatability in your infrastructure management.

Remember, this tutorial demonstrates the simplest configuration scenario. Terraform offers extensive customization and configuration options to meet more advanced infrastructure needs.

Additional Resources

Read more about: