Automating Vultr Cloud Infrastructure with Terraform

Share this article

Automating Vultr Cloud Infrastructure with Terraform

Introduction

Terraform is an open-source Infrastructure as Code (IaC) tool. It allows users to define, provision, and manage cloud infrastructure using declarative configuration files. With Terraform, you can automate the creation and management of resources such as servers, databases, networking, and storage across cloud providers.

One of Terraform’s strengths is its ability to maintain the state of infrastructure, ensuring consistency and enabling easy updates or scaling. By using version-controlled configurations, teams can collaborate more effectively, and infrastructure changes become repeatable and predictable.

This article explains how to provision Vultr Cloud Infrastructure using Terraform. You are to provision multiple resources such as cloud instances, Kubernetes Clusters, and databases using your Vultr Account API key.

Provision Vultr Resources With Terraform

Setup Terraform

  1. Download Terraform depending on your OS type.
  2. Create a Terraform directory to store the resource files.
    $ mkdir vultr-terraform
  3. Switch to the directory.
    $ cd vultr-terraform
  4. Create a new file provider.tf to store the Vultr provider information.
    $ nano provider.tf
  5. Copy and paste the below content.
    terraform {
        required_providers {
            vultr = {
                source = "vultr/vultr"
                version = "2.21.0"
            }
        }
    }
    
    provider "vultr" {
        api_key = var.VULTR_API_KEY
    }
    
    variable "VULTR_API_KEY" {}

    Save and close the file.

  6. Create a new file named terraform.tfvars to define your Vultr API key.
    $ nano terraform.tfvars
  7. Copy and paste the below directive in the file.
    VULTR_API_KEY = "vultr_api_key"
  8. Initialize Terraform to install the Vultr Terraform provider.
    $ terraform init

    Output should display a message informing Terraform has been successfully initialized.

Provision a Vultr Cloud Compute Instance

  1. Create a new file named vultr_instance.tf.
    $ nano vultr_instance.tf
  2. Copy and paste the below content.
    resource "vultr_instance" "my_instance" {
        label = "sample-server"
        plan = "vc2-1c-1gb"
        region = "sgp"
        os_id = "2284"
        enable_ipv6 = true
    }
    • vultr_instance: Sets the Vultr resource type you intend to deploy.
    • label: Specifies the instance label.
    • plan: Sets your desired instance specification. vc2-1c-1gb plan matches a Vultr instance with type vc2, 1 vCPU core, and 1 GB RAM.
    • region: Specifies your desired Vultr region to deploy the instance. sgp deploys the instance to the Singapore Vultr location.
    • os_id: Sets the instance Operating System (OS) by ID. The value 2284 represents Ubuntu 24.04.
  3. Preview the changes you are about to apply.
    $ terraform plan
  4. Create the Vultr instance.
    $ terraform apply

    When prompted, enter yes to confirm that you want to apply the changes. When successful you should be able to see the resource created in the Vultr Customer Portal.

Provision Multiple Resources at Once

  1. Create a new file named main.tf.
    $ nano main.tf
  2. Copy and paste the below content.
    resource "vultr_instance" "my_instance" {
        label       = "sample-server2"
        plan        = "vc2-1c-1gb"
        region      = "blr"
        os_id       = "2284"
        enable_ipv6 = true
    }
    
    resource "vultr_kubernetes" "kubernetes_cluster" {
        region  = "blr"
        label   = "my-cluster2"
        version = "v1.31.0+1"
    
    node_pools {
        node_quantity = 3
        plan          = "vc2-2c-4gb"
        label         = "my-app-nodes"
        auto_scaler   = true
        min_nodes     = 1
        max_nodes     = 4
        }
    }

    Save and close the file.

    This Terraform configuration defines two resources on Vultr:

    1. Vultr Cloud Compute Instance: The vultr_instance resource provisions a virtual machine (VM) labeled as “sample-server2”. The instance is configured to:
      • Use the vc2-1c-1gb plan, which provides 1 CPU and 1GB of RAM.
      • Be deployed in the Bangalore (blr) region.
      • Run Ubuntu 24.04 (specified by os_id = “2284”).
      • Enable IPv6 for the instance.
    2. Vultr Kubernetes Cluster: The vultr_kubernetes resource sets up a Kubernetes cluster labeled “my-cluster2” in the Bangalore (blr) region, with Kubernetes version v1.31.0+1. The cluster has:
      • A node pool of 3 nodes, each using the vc2-2c-4gb plan (2 CPUs and 4GB of RAM per node).
      • Auto-scaling enabled, with a minimum of 1 node and a maximum of 4 nodes in the pool.

    Together, this configuration allows for provisioning a single cloud compute instance alongside a scalable Kubernetes cluster, all managed through Terraform.

  3. Preview the changes you are about to apply.
    $ terraform plan
  4. Create the Vultr resources.
    $ terraform apply

    When prompted, enter yes to confirm that you want to apply the changes. When successful you should be able to see the resource created in the Vultr Customer Portal.

    You can also provision other Vultr Resources such as Object and Block Storage, and Vultr Managed Databases.

Do More With Vultr

This is a sponsored article by Vultr. Vultr is the world’s largest privately-held cloud computing platform. A favorite with developers, Vultr has served over 1.5 million customers across 185 countries with flexible, scalable, global Cloud Compute, Cloud GPU, Bare Metal, and Cloud Storage solutions. Learn more about Vultr

Vultr is the world’s largest privately-held cloud computing platform. A favorite with developers, Vultr has served over 1.5 million customers across 185 countries with flexible, scalable, global Cloud Compute, Cloud GPU, Bare Metal, and Cloud Storage solutions.

sponsored
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week