
Terraform is an Infrastructure as Code (IaC) tool that lets you define, manage, and provision cloud infrastructure using declarative configuration files. You can use Terraform to define resources in code and ensure that your cloud infrastructure matches the desired state by creating or modifying resources efficiently.
Terraform uses two main components to manage infrastructure:
Follow this guide to provision cloud resources on Vultr using Terraform. You will install Terraform on Ubuntu 24.04 and use it to provision multiple resources including Cloud Compute instances, Vultr Kubernetes Engine (VKE) Clusters, and Vultr Managed Databases using your Vultr API key.
Before you begin, you need to:
Terraform is not available in the default package repositories on Ubuntu. Follow the steps below to add the Terraform repository to your APT sources, update the server's package information index, and install Terraform.
Add the Terraform repository GPG key to your server's keyrings.
View the hashicorp-archive-keyring.gpg fingerprint and verify that it matches the official Hashicorp repository fingerprint.
Your output should be similar to the one below.
Add the Terraform repository to your APT sources
Update the server's package index.
Install Terraform.
View the installed Terraform version.
Your output should be similar to the one below
The Vultr Terraform provider allows you to create infrastructure resources using your Vultr API key. Follow the steps below to set up the Vultr provider configuration in your Terraform project to provision cloud resources.
Switch to your user's home directory
Create a new Terraform project directory to store your configurations.
Switch to the new vultr-terraform directory.
Create a new provider.tf configuration using a text editor such as nano to store the Vultr provider information
Add the following configurations to the file
Save and close the file.
The above provider configuration allows Terraform to interact and authenticate with the Vultr provider version 2.23.1 using your Vultr API key. Visit the Vultr Terraform Provider repository to verify the latest version to use in your configuration.
Create a new terraform.tfvars file to store your Vultr API key information.
Add the following directive to the file. Replace your_Vultr_api_key with your actual Vultr API key.
Save and close the file.
Initialize Terraform to apply the Vultr Terraform provider configuration.
Your output should be similar to the one below when successful.
You have enabled the Vultr Terraform provider configuration in your project. You can create Terraform configurations to provision resources using your Vultr API key.
Terraform uses a declarative approach that enables you to define cloud resource information in configurations files. It supports both the JSON and HCL (HarshiCorp Configuration Language) formats for configuration files. Follow the sections below to use the HCL format with the .tf file extension to provision cloud resources using your Vultr API key.
Run all Terraform commands within the project directory. Running commands outside the Terraform project directory will result into errors and failure to provision cloud resources.
Follow the steps below to provision Vultr Cloud Compute instances using Terraform.
Print your working directory and verify that it's vultr-terraform.
Your output should be similar to the one below.
Create a new vultr_instance.tf Terraform configuration.
Add the following configurations to the file.
Save and close the file.
The above Terraform configuration creates a Vultr Compute Instance with the following specifications:
VC2).1.1 GB.25 GB.2284).Within the configuration:
resource "vultr_instance" "my_instance": Creates a Vultr Cloud Compute instance. my_instance identifies the resource configuration within the Terraform project.
label = "sample-server": Sets sample-server as the instance label.
plan = "vc2-1c-1gb": Sets the instance plan and specifications. The vc2-1c-1gb plan provisions a shared cpu Vultr Cloud Compute instance with 1 vCPU, 1 GB RAM and 25 GB NVMe storage. Send a GET request to the List Plans endpoint to list all available instance plans.
region = "sgp": Sets the Vultr location to deploy the instance. The sgp region ID deploys the instance to the Singapore Vultr location. Send a GET request to the List Regions endpoint to list all available Vultr regions.
os_id = "2284": Specifies the operating system to install on the instance. 2284 installs Ubuntu 24.04. Send a GET request to the List OS endpoint to list all available operating systems you can install.
enable_ipv6 = true: Enables IPV6 networking on the Vultr Cloud Compute instance.
View the Terraform configuration changes to apply to your Vultr account.
Output:
Apply the Terraform configuration changes to provision the Vultr Cloud Compute instance
Enter yes when prompted to apply the changes.
Your output should be similar to the one below when successful.
You have provisioned an Ubuntu 24.04 Vultr Cloud Compute instance. Use the Vultr Console, Vultr API, or Vultr CLI to view the instance details including the default user credentials, usage statistics and manage additional settings.
Follow the steps below to provision Vultr Kubernete Engine (VKE) clusters using Terraform.
Create a new kubernetes_cluster.tf Terraform configuration to store your Kubernetes resource information.
Add the following configurations to the file. Replace my-cluster with your desired VKE cluster name.
Save and close the file.
The above Terraform configuration creates a new Vultr Kubernetes Engine (VKE) cluster with the following cluster specifications:
v1.28.2+13vc2) with 2 vcPUs, 4GB RAM per node.my-app-nodesenabledWithin the configuration:
resource "vultr_kubernetes" "first_kubernetes_cluster": Creates a Vultr Kubernetes Engine (VKE) cluster. first_kubernetes_cluster identifies the VKE resource configuration within the Terraform project.
region = "ewr": Specifies the Vultr location to deploy the VKE cluster. ewr deploys the Vultr Kubernetes Engine cluster in the Vultr New Jersey (ewr) location. Send a GET request to the List Regions endpoint to list all available Vultr regions.
label = "my-cluster": Specifies the VKE cluster label for identification in your Vultr account.
version = "v1.32.1+1": Specifies a Kubernetes version to install on the VKE cluster. Send a GET request to the Get Kubernetes Versions endpoint to list the available Kubernetes versions.
node_pools {: Creates a new VKE nodes pool configuration.
node_quantity = 3: Specifies the number of nodes to add to the cluster. 3 adds three worker nodes to the VKE cluster.
plan = "vc2-2c-4gb": Specifies the node instance plan.
label = "my-app-nodes": Specifies the node labels for identification within the VKE cluster.
auto_scaler = true: Enables automatic scaling. Setiing the value to false disables automatic scaling.
min_nodes = 1: Sets the minimum number of nodes the VKE cluster can downscale to. 1 limits downscaling to a single node.
max_nodes = 4: Sets the maximum number of nodes the VKE cluster can upscale to. 4 allows the VKE cluster to scale up-to four nodes.
View the Terraform configuration changes.
Apply the Terraform configuration to create the VKE cluster.
Enter yes when prompted to apply the Terraform configuration changes and create the VKE cluster. Your output should be similar to the one below when successful.
You have provisioned a Vultr Kubernetes Engine (VKE) cluster with 3 nodes. Use the Vultr Console or the Vultr API to download your VKE cluster configuration, manage nodes, upgrades and linked resources.
Modifying a Terraform configuration applies the changes to your existing resource. Follow the steps below to modify the Vultr Kubernetes Engine (VKE) configuration to add more node pools to the cluster.
Open the kubernetes_cluster.tf file.
Add the following configurations at the end of the file to define a new linked resource.
Save and close the file.
The above configuration creates a new VKE node pool with the following specifications per node:
1.vc2) with 4 vCPUs, 8 GB RAM.enabled.Within the configuration:
resource "vultr_kubernetes_node_pools" "additional_node_pools" {: Creates a new VKE cluster node pool configuration. additional_node_pools identifies the resource configuration within the Terraform project.cluster_id = "${vultr_kubernetes.first_kubernetes_cluster.id}": References the VKE cluster ID in your configuration to add the node pool.node_quantity = 1: Specifies the number of nodes to add to the cluster.plan = "vc2-4c-8gb": Specifies the node instance plan.label = "additional-node-pool": Specifies the nodes pool label.tag = "additional-node-pool": Specifies the node tag.auto_scaler = true: Enables automatic scaling.min_nodes = 1: Specifies the minimum number of nodes to downscale to. 1 limits downscaling to a single node.max_nodes = 2: Specifies the maximum number of nodes to upscale to. 2 enables upscaling to up to two nodes.Review the Terraform configuration changes to apply.
Apply the Terraform configuration changes to your VKE cluster.
Your output should be similar to the one below when successful.
You have added new nodes to an existing VKE cluster. You can use Terraform to modify the Vultr Kubernetes Engine (VKE) cluster configuration to add new nodes and resources. Use the Vultr Console, Vultr API, or Vultr CLI to verify the available node pools information.
Follow the steps below to provision Vultr Managed Databases using Terraform.
Create a new database.tf Terraform configuration to store the Vultr Managed Databases resource information.
Add the following configurations to the file.
Save and close the file.
The above Terraform configuration creates a Vultr Managed Databases for PostgreSQL cluster with the following specifications:
pg).15.vultr-dbaas-startup-cc-1-55-2 - Vultr Cloud Compute (cc) with 1 vCPU, 2 GB RAM, 55 GB storage.Within the configuration:
resource "vultr_database" "prod_postgresql_database": Creates a Vultr Managed Databases cluster. The prod_postgresql_database label indentifies the resource configuration within the Terraform project.
database_engine = "pg": Specifies the Vultr Managed Databases engine. pg creates a Vultr Managed Databases for PostgreSQL cluster.
database_engine_version = "15": Specifies the Vultr Managed Databases engine version.
region = "ams": Specifies the Vultr location to deploy the Vultr Managed Databases cluster. ams deploys the Vultr Managed Databases cluster in the Amsterdam Vultr location.
plan = "vultr-dbaas-startup-cc-1-55-2": Specifies the Vultr Managed Databases instance plan. Send a GET request to the List Managed Database Plans endpoint to list all available Vultr Managed Databases plans.
label = "my-production-db-label": Specifies the Vultr Managed Databases label for identification in your Vultr account.
Review the Terraform configuration changes to apply.
Apply the Terraform configuration changes to deploy the Vultr Managed Databases for PostgreSQL cluster.
Your output should be similar to the one below when successful.
You have deployed a Vultr Managed Databases for PostgreSQL cluster. Use the Vultr Console, Vultr API, or Vultr CLI to manage the cluster.
Follow the steps below to modify the Vultr Managed Databases configuration to create a new user in the cluster.
Open the database.tf Terraform configuration file.
Add the following configurations at the end of the file.
Save and close the file.
The above configuration creates a new Vultr Managed Databases for PostgreSQL user with the following information.
adminuserstrongpasswordWithin the configuration:
resource "vultr_database_user" "new_database_user": Creates a Vultr Managed Databases user configuration. new_database_user identifies the resource within the Terraform project.database_id = "${vultr_database.prod_valkey_database.id}": Specifies the target Vultr Managed Databases ID to create the user. ${vultr_database.prod_valkey_database.id} references the Vultr Managed Databases ID you created earlier using the Terraform configuration.username = "adminuser": Specifies the user's name.password = "strongassword": Specifies the user password.Review the Terraform configuration changes to apply.
Apply the Terraform configuration changes to create the Vultr Managed Databases user.
Output:
You have created a Vultr Managed Databases user. Use the Vultr Console to manage all Vultr Managed Databases users and modify the user permissions in your cluster.
Follow the steps below to provision Vultr Object Storage subscriptions using Terraform.
Create a new object_storage.tf Terraform configuration file to store the Vultr Object Storage resource information.
Add the following configurations to the file.
Save and close the file.
The above Terraform resource configuration creates a new Vultr Object Storage subscription with the following details:
4).Within the configuration:
resource "vultr_object_storage" "my_object_storage": Creates a new Vultr Object Storage subscription resource. my_object_storage identifies the resource configuration within the Terraform project.
cluster_id = 4: Specifies the Vultr region ID to create the Vultr Object Storage subscription. Send a GET request to the Get All Clusters endpoint to list all Vultr locations where Vultr Object Storage is available.
label = "Example Object Storage": Specifies the Vultr Object Storage label.
Review the Terraform configuration changes to apply.
Apply the Terraform resource configuration to create the Vultr Object Storage subscription.
Output:
You have provisioned a Vultr Object Storage subscription. Use the Vultr Console to manage the subscription and use S3-compatible tools such as s3cmd to create buckets.
Follow the steps below to provision Vultr Block Storage volumes using Terraform.
Create a new block_storage.tf Terraform configuration to store the Vultr Block Storage resource information.
Add the following configurations to the file.
Save and close the file.
The above resource configuration creates a Vultr Block Storage volume with the following specifications:
10 GB.sgp).New Block Storage.Within the configuration:
resource "vultr_block_storage" "my_block_storage": Creates a Vultr Block Storage resource configuration. my_block_storage identifies the resource within the Terraform project.size_gb = 10: Specifies the Vultr Block Storage volume size. 10 creates a 10 GB Vultr Block Storage volume.region = "sgp": Specifies the Vultr region ID to deploy the Vultr Block Storage volume. sgp deploys Vultr Block Storage in the Singapore Vultr Location.label = "New Block Storage": Specifies the Vultr Block Storage label.Review the Terraform configuration changes to apply.
Apply the Terraform configuration to create the Vultr Block Storage volume.
Output:
You have provisioned a Vultr Block Storage volume using Terraform. Use the Vultr Console, API or CLI to manage the volume.
Follow the steps below to modify the Vultr Block Storage configuration and attach the volume to a Vultr Cloud Compute Instance.
Open the block_storage.tf file.
Add a new attached_to_instance directive to the my_block_storage resource. Replace my_instance.id with the actual Vultr Cloud Compute instance resource label you created earlier.
Save and close the file.
The above configuration references the Vultr Cloud Compute instance resource to attach the Vultr Block Storage volume. Within the configuration:
attached_to_instance = "${vultr_instance.my_instance.id}: Specifies the instance the Vultr Block Storage volume is attached to. vultr_instance.my_instance.id references the my_instance Vultr Cloud Compute instance ID you created earlier.Apply the Terraform configuration changes to attach the Vultr Block Storage volume to the Vultr Cloud Compute instance.
Your output should be similar to the one below.
You have attached a Vultr Block Storage volume to a Vultr Cloud Compute instance. Use the Vultr Console to verify that the Vultr Block Storage volume is attached to the instance. Then, follow How to Mount Vultr Block Storage Volume on Linux to use the volume.
Follow the steps below to destroy existing cloud resources using Terraform.
Use the following command to destoy a specific cloud resource on your Vultr account.
Verify the resources to destroy, enter yes, and press Enter to destroy the target resource when prompted in your output similar to the one below.
Use the destroy command option to destroy all cloud resources in your Terraform project.
Verify the cloud resources to destroy, enter yes and press Enter to destroy the resources listed your output similar to the one below.
Running the terraform destroy destroys all resources provisioned through the Terraform project. Destroy specific resources by specifying the configuration resource and label to avoid losing existing instances instead.
Terraform supports multiple operation commands to initialize, validate, and apply cloud resources to your Vultr account. These include the following:
Init: Initializes the Terraform project directory and installs the defined provider plugins.Refresh: Reads the state of cloud resources, and updates the Terraform state file to match the resource status.Validate: Looks up for any syntax, formatting errors, or wrong configurations in Terraform resource files.Plan: Lists the Terraform changes made to configuration resources and ready to apply on your Vultr account.Apply: Applies the Terraform resource configurations.Destroy: Destroys existing cloud resources deployed using Terraform in the active project directory.For more information and supported commands, visit the Terraform CLI documentation
You have installed Terraform and used the Vultr provider configuration to provision cloud resources using your Vultr API key. You can create multiple project directories to categorize and store Terraform configurations on your server. For more information, visit the Vultr Terraform registry
0 Comments
Be the first to comment and share your perspective with the community.