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
- Download Terraform depending on your OS type.
- Create a Terraform directory to store the resource files.
$ mkdir vultr-terraform
- Switch to the directory.
$ cd vultr-terraform
- Create a new file
provider.tf
to store the Vultr provider information.$ nano provider.tf
- 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.
- Create a new file named
terraform.tfvars
to define your Vultr API key.$ nano terraform.tfvars
- Copy and paste the below directive in the file.
VULTR_API_KEY = "vultr_api_key"
- 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
- Create a new file named
vultr_instance.tf
.$ nano vultr_instance.tf
- 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.
- Preview the changes you are about to apply.
$ terraform plan
- 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
- Create a new file named
main.tf
.$ nano main.tf
- 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:
- 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.
- 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.
- Vultr Cloud Compute Instance: The
- Preview the changes you are about to apply.
$ terraform plan
- 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
- Install Node.js and NPM on Rocky Linux 9.
- Install Python and Pip on Ubuntu 24.04.
- Install Podman on Ubuntu 24.04.
- Install Docker on Rocky Linux 9.
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.